Android Example Change Text On Button Click

      No Comments on Android Example Change Text On Button Click
MainActivity.java
package com.example.scanfcode;
import android.os.Bundle;
import android.widget.*;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity implements OnClickListener
{ 
	Button b1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		b1=(Button)findViewById(R.id.button1);
		b1.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v)
	{
		if(v.getId()==R.id.button1)
		{
			b1.setText("After Click");
		}
	}


}

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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="92dp"
        android:text="ScanfCode" />

</RelativeLayout>

Before Click:-

scanfcode1

After Click:-

scanfcode2

Leave a Reply

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