In this android example,we learn basic of how to set background of layout using drawable resource.
here we set background of layout which is display as Image button.
CREATE NEW ANDROID PROJECT
Step 1: write code into activity_main.xml
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#17577e"
android:orientation="vertical"
android:id="@+id/demo" >
<ImageButton
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="mess"
android:text="First Image "
android:src="@drawable/a"
android:textSize="10sp" />
<ImageButton
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="mess"
android:text="Second Image"
android:src="@drawable/b"
android:textSize="10sp" />
</LinearLayout>
Step 2: write code into your MainActivity.java
package dev.androidapplink.imagechange;
import android.R.layout;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// This function will call when we click on any button
// mess method is declared in XML file
public void mess(View v) {
// initialize image view object
LinearLayout ly = (LinearLayout) findViewById(R.id.demo);
// get clicked button id from view object
switch (v.getId()) {
case R.id.button1:
// if button1 is clicked than set image a
ly.setBackgroundResource(R.drawable.a);
break;
case R.id.button2:
// if button2 is clicked than set image b
ly.setBackgroundResource(R.drawable.b);
break;
}
}
// private layout findViewById(Class<dev.androidapplink.imagechange.R.layout> class1) {
// TODO Auto-generated method stub
// return null;
// }
}
NOTE: Put two image with named is (1) a.jpg and (2) b.jpg into ...rec/drawable-hdpi.
NOTE: No needed special permission on AndroidManifest.xml
Step 3: Now Run Your Project:
Step 3: Now Run Your Project:
This is a great post. I like this topic.This site has lots of advantage.I found many interesting things from this site. It helps me in many ways.Thanks for posting this again.
ReplyDeletebackground verification in bangalore
Thank you sir :)
Delete