Skip to content

Commit 6efefbe

Browse files
author
Nilanchala
committed
Added Custom and Compound Views in Android Tutorial
Custom and Compound Views in Android Tutorial
1 parent 33ee204 commit 6efefbe

File tree

21 files changed

+331
-0
lines changed

21 files changed

+331
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.example.customview"
4+
android:versionCode="1"
5+
android:versionName="1.0" >
6+
7+
<uses-sdk
8+
android:minSdkVersion="8"
9+
android:targetSdkVersion="17" />
10+
11+
<application
12+
android:allowBackup="true"
13+
android:icon="@drawable/ic_launcher"
14+
android:label="@string/app_name"
15+
android:theme="@style/AppTheme" >
16+
<activity
17+
android:name="com.example.customview.MainActivity"
18+
android:label="@string/app_name" >
19+
<intent-filter>
20+
<action android:name="android.intent.action.MAIN" />
21+
22+
<category android:name="android.intent.category.LAUNCHER" />
23+
</intent-filter>
24+
</activity>
25+
</application>
26+
27+
</manifest>
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# To enable ProGuard in your project, edit project.properties
2+
# to define the proguard.config property as described in that file.
3+
#
4+
# Add project specific ProGuard rules here.
5+
# By default, the flags in this file are appended to flags specified
6+
# in ${sdk.dir}/tools/proguard/proguard-android.txt
7+
# You can edit the include path and order by changing the ProGuard
8+
# include property in project.properties.
9+
#
10+
# For more details, see
11+
# http://developer.android.com/guide/developing/tools/proguard.html
12+
13+
# Add any project specific keep options here:
14+
15+
# If your project uses WebView with JS, uncomment the following
16+
# and specify the fully qualified class name to the JavaScript interface
17+
# class:
18+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19+
# public *;
20+
#}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# This file is automatically generated by Android Tools.
2+
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3+
#
4+
# This file must be checked in Version Control Systems.
5+
#
6+
# To customize properties used by the Ant build system edit
7+
# "ant.properties", and override values to adapt the script to your
8+
# project structure.
9+
#
10+
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11+
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12+
13+
# Project target.
14+
target=android-17
Loading
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
xmlns:custom="http://schemas.android.com/apk/res/com.example.customview"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:layout_margin="10dp"
7+
android:orientation="vertical"
8+
tools:context=".MainActivity" >
9+
10+
<com.example.customview.LovelyView
11+
android:id="@+id/lovelyView"
12+
android:layout_width="fill_parent"
13+
android:layout_height="wrap_content"
14+
android:layout_margin="10dp"
15+
custom:leftLabel=""
16+
custom:leftLabelStyle="@style/leftTextStyle"
17+
custom:rightLabel=""
18+
custom:rightLabelStyle="@style/rightTextStyle" />
19+
20+
<com.example.customview.LovelyView
21+
android:id="@+id/lovelyView1"
22+
android:layout_width="fill_parent"
23+
android:layout_height="wrap_content"
24+
custom:leftLabel="Hello Mr:"
25+
custom:leftLabelStyle="@style/leftTextStyle"
26+
custom:rightLabel="Whats Up Buddy!"
27+
custom:rightLabelStyle="@style/rightTextStyle" />
28+
29+
</LinearLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="horizontal"
6+
android:weightSum="2" >
7+
8+
<TextView
9+
android:id="@+id/leftTextView"
10+
android:layout_width="fill_parent"
11+
android:layout_height="wrap_content"
12+
android:layout_weight="1"
13+
android:gravity="right|center_vertical"
14+
android:text="key" />
15+
16+
17+
<TextView
18+
android:id="@+id/rightTextView"
19+
android:layout_width="fill_parent"
20+
android:layout_height="wrap_content"
21+
android:layout_weight="1"
22+
android:gravity="left|center_vertical"
23+
android:text="value" />
24+
25+
</LinearLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
2+
3+
<item
4+
android:id="@+id/action_settings"
5+
android:orderInCategory="100"
6+
android:showAsAction="never"
7+
android:title="@string/action_settings"/>
8+
9+
</menu>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<resources>
2+
3+
<!--
4+
Customize dimensions originally defined in res/values/dimens.xml (such as
5+
screen margins) for sw600dp devices (e.g. 7" tablets) here.
6+
-->
7+
8+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<resources>
2+
3+
<!--
4+
Customize dimensions originally defined in res/values/dimens.xml (such as
5+
screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.
6+
-->
7+
<dimen name="activity_horizontal_margin">128dp</dimen>
8+
9+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<resources>
2+
3+
<!--
4+
Base application theme for API 11+. This theme completely replaces
5+
AppBaseTheme from res/values/styles.xml on API 11+ devices.
6+
-->
7+
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
8+
<!-- API 11 theme customizations can go here. -->
9+
</style>
10+
11+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<resources>
2+
3+
<!--
4+
Base application theme for API 14+. This theme completely replaces
5+
AppBaseTheme from BOTH res/values/styles.xml and
6+
res/values-v11/styles.xml on API 14+ devices.
7+
-->
8+
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
9+
<!-- API 14 theme customizations can go here. -->
10+
</style>
11+
12+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<declare-styleable name="LovelyView">
5+
<attr name="leftLabel" format="string" />
6+
<attr name="rightLabel" format="string" />
7+
<attr name="leftLabelStyle" format="reference" />
8+
<attr name="rightLabelStyle" format="reference" />
9+
</declare-styleable>
10+
11+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<resources>
2+
3+
<!-- Default screen margins, per the Android Design guidelines. -->
4+
<dimen name="activity_horizontal_margin">16dp</dimen>
5+
<dimen name="activity_vertical_margin">16dp</dimen>
6+
7+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<string name="app_name">CustomView</string>
5+
<string name="action_settings">Settings</string>
6+
<string name="hello_world">Hello world!</string>
7+
8+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<resources xmlns:android="http://schemas.android.com/apk/res/android">
2+
3+
<!--
4+
Base application theme, dependent on API level. This theme is replaced
5+
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
6+
-->
7+
<style name="AppBaseTheme" parent="android:Theme.Light">
8+
<!--
9+
Theme customizations available in newer API levels can go in
10+
res/values-vXX/styles.xml, while customizations related to
11+
backward-compatibility can go here.
12+
-->
13+
</style>
14+
15+
<!-- Application theme. -->
16+
<style name="AppTheme" parent="AppBaseTheme">
17+
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
18+
</style>
19+
20+
<style name="leftTextStyle">
21+
<item name="android:textSize">18dp</item>
22+
<item name="android:textColor">#ff0000</item>
23+
<item name="android:textStyle">normal</item>
24+
</style>
25+
26+
<style name="rightTextStyle">
27+
<item name="android:textSize">22dp</item>
28+
<item name="android:textColor">#0000ff</item>
29+
<item name="android:textStyle">bold</item>
30+
</style>
31+
32+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.example.customview;
2+
3+
import android.content.Context;
4+
import android.content.res.TypedArray;
5+
import android.util.AttributeSet;
6+
import android.view.LayoutInflater;
7+
import android.widget.LinearLayout;
8+
import android.widget.TextView;
9+
10+
public class LovelyView extends LinearLayout {
11+
private String leftLabel = "";
12+
private String rightLabel = "";
13+
private TextView leftTextView;
14+
private TextView rightTextView;
15+
private int leftStyle ;
16+
private int rightStyle;
17+
18+
19+
public LovelyView(Context context) {
20+
super(context);
21+
LayoutInflater.from(context).inflate(R.layout.key_value_layout, this);
22+
}
23+
24+
public LovelyView(Context context, AttributeSet attrs) {
25+
super(context, attrs);
26+
initViews(context, attrs);
27+
}
28+
29+
public LovelyView(Context context, AttributeSet attrs, int defStyle) {
30+
this(context, attrs);
31+
initViews(context, attrs);
32+
}
33+
34+
35+
36+
private void initViews(Context context, AttributeSet attrs) {
37+
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
38+
R.styleable.LovelyView, 0, 0);
39+
40+
try {
41+
// get the text and colors specified using the names in attrs.xml
42+
leftLabel = a.getString(R.styleable.LovelyView_leftLabel);
43+
rightLabel = a.getString(R.styleable.LovelyView_rightLabel);
44+
leftStyle = a.getResourceId(R.styleable.LovelyView_leftLabelStyle, android.R.style.TextAppearance_DeviceDefault);
45+
rightStyle = a.getResourceId(R.styleable.LovelyView_rightLabelStyle, android.R.style.TextAppearance_DeviceDefault);
46+
47+
} finally {
48+
a.recycle();
49+
}
50+
51+
LayoutInflater.from(context).inflate(R.layout.key_value_layout, this);
52+
53+
//left text view
54+
leftTextView = (TextView) this.findViewById(R.id.leftTextView);
55+
leftTextView.setText(leftLabel);
56+
leftTextView.setTextAppearance(context, leftStyle);
57+
58+
//right text view
59+
rightTextView = (TextView) this.findViewById(R.id.rightTextView);
60+
rightTextView.setText(rightLabel);
61+
rightTextView.setTextAppearance(context, rightStyle);
62+
}
63+
64+
public String getLeftLabel() {
65+
return leftLabel;
66+
}
67+
68+
public void setLeftLabel(String leftLabel) {
69+
this.leftLabel = leftLabel;
70+
if(leftTextView!=null){
71+
leftTextView.setText(leftLabel);
72+
}
73+
}
74+
75+
public String getRightLabel() {
76+
return rightLabel;
77+
}
78+
79+
public void setRightLabel(String rightLabel) {
80+
this.rightLabel = rightLabel;
81+
if(rightTextView!=null){
82+
rightTextView.setText(rightLabel);
83+
}
84+
}
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.example.customview;
2+
3+
import android.os.Bundle;
4+
import android.app.Activity;
5+
import android.view.Menu;
6+
7+
public class MainActivity extends Activity {
8+
9+
@Override
10+
protected void onCreate(Bundle savedInstanceState) {
11+
super.onCreate(savedInstanceState);
12+
setContentView(R.layout.activity_main);
13+
14+
LovelyView view = (LovelyView) findViewById(R.id.lovelyView);
15+
view.setLeftLabel("Left TextField");
16+
view.setRightLabel("Right TextField");
17+
}
18+
19+
@Override
20+
public boolean onCreateOptionsMenu(Menu menu) {
21+
getMenuInflater().inflate(R.menu.main, menu);
22+
return true;
23+
}
24+
}

0 commit comments

Comments
 (0)