How to design the style/theme in Android

In this tutorial how to design the style/theme in Android is shown.

Code:

activity_main.xml

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity” >

<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”107dp”
android:text=”Program of Theme” />

<Button
android:id=”@+id/back”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/textView1″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”93dp”
android:onClick=”onSubmit”
android:text=”Submit” />

<EditText
android:id=”@+id/et1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/textView1″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”29dp”
android:ems=”10″
android:hint=”Enter your name” >

<requestFocus />
</EditText>

</RelativeLayout>

MainActivity.java

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity{

EditText nm;
String n;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nm=(EditText)findViewById(R.id.et1);
}

public void onSubmit(View v)
{
n=nm.getText().toString();
Intent in=new Intent(this,Second.class);
in.putExtra(“NM”,n);
startActivity(in);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

second_layout.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:onClick=”onBack” >

<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”116dp”
android:text=”TextView” />

<Button
android:id=”@+id/back”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/textView1″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”45dp”
android:text=”Back” />

</RelativeLayout>

Second.java

package com.example.demo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class Second extends Activity{

TextView nm;
String n;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
nm=(TextView)findViewById(R.id.textView1);
n=getIntent().getStringExtra(“NM”);
nm.setText(n);
}

public void onBack(View v)
{
Intent in=new Intent(this,MainActivity.class);
startActivity(in);
}
}

styles.xml

<resources xmlns:android=”http://schemas.android.com/apk/res/android”>

<!–
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
–>
<style name=”AppBaseTheme” parent=”android:Theme.Light”>
<!–
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
–>
</style>

<!– Application theme. –>
<style name=”AppTheme” parent=”AppBaseTheme”>
<!– All customizations that are NOT specific to a particular API-level can go here. –>
</style>

<style name=”mystyle”>
<item name=”android:textColor”>#008000</item>
<item name=”android:textSize”>20px</item>
<item name=”android:background”>#ff2200</item>
</style>

</resources>

AndroidManifest.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.demo”
android:versionCode=”1″
android:versionName=”1.0″ >

<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”17″ />

<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”com.example.demo.MainActivity”
android:label=”@string/app_name”
android:theme=”@style/mystyle” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.Second”></activity>
</application>

</manifest>

Add a Comment

Your email address will not be published. Required fields are marked *