Open Eclipse IDE.
Click File>New>Android Application Project.
Fill the Application Name "AndroidButton"
Fill the Project Name "AndroidButton"
The package name is of type "com.codingredefined.androidbutton"
Then click next.
Then Configure Project window opens. Keep the default settings and click next.
Now you can choose the icon for your app from the provided icons or can choose a file from your system.
Again click next.
Now give your activity(ie the fie in which all the logic is to be performed) a name "MainActivity", then the layout name "activity_main" and click Finish.
Open the layout folder in the res folder.
Add the following code to activity_main.xml:
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World" />
<Button
android:id="@+id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv"
android:text="Click Me" />
The above code will make the button appear on screen.
Now to make the Button working
Open MainActivity.java and write:
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity
{
TextView tv;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.tv);
b=(Button)findViewById(R.id.b);
b.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
tv.setText("Button Clicked...!");
}
});
}
@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;
}
}
Now save both the layout and activity file.
Now in the Package Explorer right click your app name
Click Run As>Android Application.
Your application should look like this:
Click File>New>Android Application Project.
Fill the Application Name "AndroidButton"
Fill the Project Name "AndroidButton"
The package name is of type "com.codingredefined.androidbutton"
Then click next.
Then Configure Project window opens. Keep the default settings and click next.
Now you can choose the icon for your app from the provided icons or can choose a file from your system.
Again click next.
Now give your activity(ie the fie in which all the logic is to be performed) a name "MainActivity", then the layout name "activity_main" and click Finish.
Open the layout folder in the res folder.
Add the following code to activity_main.xml:
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World" />
<Button
android:id="@+id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv"
android:text="Click Me" />
The above code will make the button appear on screen.
Now to make the Button working
Open MainActivity.java and write:
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity
{
TextView tv;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.tv);
b=(Button)findViewById(R.id.b);
b.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
tv.setText("Button Clicked...!");
}
});
}
@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;
}
}
Now in the Package Explorer right click your app name
Click Run As>Android Application.
Your application should look like this:





















