Thursday 21 February 2013

Android Button with text and image dynamically Part-2

Part-2
In previous blog i add image with text to Button through layout i.e statically.
Now i will show you how to add text with image to Button view dynamically.

Screen Shots:- Before
























Screen Shots 2:--
























Steps:--1

Create an Android project.

layout/activity_main.xml

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:paddingTop="50dp"
        android:text="@string/hello_world"
        android:textSize="16sp" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal"
        android:padding="10dp" >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/buttonleft"
        android:layout_gravity="center_horizontal"
        android:text="@string/addimagesleft" />
     <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/buttonright"
        android:layout_gravity="center_horizontal"
        android:text="@string/addimagesright" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal"
        android:padding="10dp" >

        <!-- For Image to the left of the text use android:drawableLeft tag -->

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/butleft"
            android:layout_margin="10dp"
            android:background="@drawable/menu"
            android:drawablePadding="5dp"
            android:text="@string/leftimage" />
        <!-- For Image to the Right of the text use android:drawableRight tag -->

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/butright"
            android:layout_margin="10dp"
            android:background="@drawable/menu"
            android:drawablePadding="5dp"
            android:text="@string/righttimage" />
    </LinearLayout>

  </LinearLayout>



Step 2) src/MainActivity.java

package com.amit.button;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    private Button setleftBut,setrightbut,buttonwithleft,buttonwithright;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setleftBut=(Button)findViewById(R.id.buttonleft);
        setrightbut=(Button)findViewById(R.id.buttonright);
       
        buttonwithleft=(Button)findViewById(R.id.butleft);
        buttonwithright=(Button)findViewById(R.id.butright);
       
        setleftBut.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {
                buttonwithleft.setCompoundDrawablesWithIntrinsicBounds(R.drawable.up_arrow, 0,
                        0, 0);
            }
        });
       
        setrightbut.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {
                buttonwithright.setCompoundDrawablesWithIntrinsicBounds(0, 0,
                        R.drawable.up_arrow, 0);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}


Step:3)
values/strings.xml

     <string name="app_name">ButtonWithImage</string>
    <string name="hello_world">Buttom with Images Dynamically</string>
    <string name="menu_settings">Settings</string>
    <string name="leftimage">LeftImage</string>
    <string name="righttimage">RightImage</string>
     <string name="addimagesleft">Add Image To the Left</string>
    <string name="addimagesright">Add Image To the Right</string>


Download full Source code Click ButtonWithImageDynamically

1 comment:


  1. You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting

    for your next post keep on updating these kinds of knowledgeable things...

    Android App Development Company

    ReplyDelete