About Button in Layout - android

I placed a Button in a layout
here is my xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:textColor="#ff0000"/>
and wrote the following code in activity:
public class Basic extends Activity {
Button btn;
public void oncreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.first);
btn = (Button) findViewById(R.id.button1);
}
public void clkBtn(View v) {
Toast.makeText(this, "hai.........", Toast.LENGTH_SHORT).show();
}
}
When I run this code, I am getting white blank screen (without any button). Can any one tell me what is wrong with my code?

Update your first.xml under res->layout folder like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clkBtn"
android:text="Click Me" />
</LinearLayout>

To get the Button event you need to first register your Button for that. The code which you have written is right but to invoke that method you have to add the property android:onClick="clkBtn" in your layout file.
OR
If you don't want to use this way then you can also explicitly invoke the event by registering your Button in your class as below:
public class Basic extends Activity {
Button btn;
public void oncreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.first);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Toast.makeText(this, "hai.........", Toast.LENGTH_SHORT).show();
}
}
And also to launch your activity make sure have added your activity as launcher in your manifest file as below .
<activity android:label="#string/app_name"
android:name="Basic" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

This is My Answer. In XML file Button should be like this:
<Button
android:id="#+id/ID of you button "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name To Display Button name" />
In the MainActivity OR In any activity (in which you have called XML):
public class MainActivity extends Activity {
public void oncreate(Bundle paramBundle) {
super.onCreate(paramBundle);
setContentView(R.layout."Your XML file in which Button is.");
Button btn = (Button)findViewById(R.id."Your button Id");
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Do your onclick program here
}
});
}
}

Related

Android - Button under Relative layout wont initiate OnClickListener when clicked

Hello this is my xml file
<RelativeLayout
android:id="#+id/tutorialBox"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="15dip"
android:paddingBottom="15dip">
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
</RelativeLayout>
i have made an on click listener for it
final Button closeBt = (Button) findViewById(R.id.closeBen);
closeBt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
closeBt.setText("Im a button");
}
});
for some reason when i click this button nothing happens it doesnt look like it has been clicked.
when i took the button out of the realtive layout everything worked fine
any suggestions?
Instead of this add onClick attribute to the button tag in xml.
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:onClick = "close_clicked"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
Then in Main Activity just make a new method like this.
public void close_clicked (View v){
// Your code
}
No need to add on click listner.
Your RelativeLayout does not look good, is it your main container? Maybe try it this way
<?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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
</RelativeLayout>
And a good practice is to declare your widgets globally then instantiate them in theOnCreate
public class Foo extends AppCompatActivity {
private Button closeBenny;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
closeBenny = (Button)findViewById(R.id.closeBen);
closeBenny.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
closeBenny.setText("Im a button");
}
});
}
}

Android Second activity does not load

Sorry for my English.
I'm trying to use a second activity in Android, but it doesn't load. It's like the code jump it. Below, you can see the code. Thanks.
1- First Activity
public class MainActivity extends Activity
{
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.btenviardados);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helooo");
Intent intent = new Intent(MainActivity.this, com.example.seven.reader.activity_janela1.class);
startActivity(intent);
System.out.println("ebadasdadas");
}
});
}
2- Second Activity
public class activity_janela1 extends Activity
{
public void OnCreate(Bundle saveInstaceState)
{
super.onCreate(saveInstaceState);
setContentView(R.layout.activity_janela1);
}}
3- First layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I&apos;m screen 1 (main.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/btenviardados"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me to another screen" />
4- Second layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/layoutFormulario"
android:orientation="vertical">
</LinearLayout>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Im screen 2 (main2.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
5- AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.seven.reader"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".MainActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name="com.example.seven.reader.activity_janela1" >
</activity>
</application>
</manifest>
You have created a method within your oncreate. Remove that and leave it in onCreate.
///public void addListenerOnButton() {
button = (Button) findViewById(R.id.btenviardados);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helooo");
Intent intent = new Intent(MainActivity.this, com.example.seven.reader.activity_janela1.class);
startActivity(intent);
System.out.println("ebadasdadas");
}
});
You are misunderstanding how to use methods and scope. I recommend you do a read up on these things.
The problem is with your method addListenerOnButton().
You could define a method which will be executed when button is clicked via xml just add onClick attribute where you have defined your button.
As:-
<Button
android:id="#+id/btenviardados"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me to another screen"
android:onClick="click" />
And define click method in MainActivity.java
as:-
public void click(View view)
{
System.out.println("helooo");
Intent intent = new Intent(MainActivity.this, com.example.seven.reader.activity_janela1.class);
startActivity(intent);
System.out.println("ebadasdadas");
}
Or you can add OnClickListener in you onCreate method of MainActivity.
Try this, may help you:
public class MainActivity extends Activity
{
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btenviardados);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helooo");
Intent intent = new Intent(getApplicationContext, activity_janela1.class);
startActivity(intent);
finish();
System.out.println("ebadasdadas");
}
});
}
}

How to create a buttonlistener for an inflated button

Am quite new to Android programming and hope someone can help me out in this:
1. I am coding from one of the demos here:
http://developer.android.com/training/animation/screen-slide.html
I have a main activity_screen_slide.xml file:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
In another xml file, main.xml, I have a few buttons.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="#+id/buttonStart"
android:text="test"/>
</LinearLayout>
Within the main java activity file, the context is set as following:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
When I tried to set a listener for testButton, am unable to do so. This means that when the button is generated subsequently (through an inflator in another java file), there is no response when I click the button.
The java file to generate the inflator is simply:
ViewGroup rootView;
rootView = (ViewGroup) inflater.inflate(R.layout.main, container, false);
Thank you and appreciate your help
Cheers
D
I assume your button is defined in your xml layout activity_screen_slide.xml similar to this:
<Button
android:id="#+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
In your Activity you can add a ClickListener like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("", "I've been clicked");
}
});
}
Try this:
public class Settings extends Activity implements View.OnClickListener {
public void onCreate(Bundle savedInstanceState) {
//...ALL THE OTHER CODE..\\
Button button = (Button) findViewById(R.id.YOUR_BUTTON_ID);
button.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.YOUR_BUTTON_ID: {
ALPHA.YourFunction(parameters);
}
}
}
}

How to make a button redirect to another XML layout

I'm making a button in xml(res / layout / activity_home.xml), like this:
<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"
tools:context=".HomeActivity" >
<ImageView
android:id="#+id/imageView1"
android:src="#drawable/schkopwide"
android:contentDescription="#string/HTI"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="78dp"
android:onclick="Intent i = new Intent(activity_store.xml);
startActivity(i);"
android:text="#string/HTI" />
</RelativeLayout>
so what should I add into this xml to let it redirect to another xml page (res / layout / activity_store.xml)?
Thank you
If you Want to show two different layouts in Same Activity, then ViewSwitcher is best layout.
You can add multiple layouts in ViewSwithcher. And Replace them by using viewswitcher.next(); function.
<ViewSwitcher
android:id="#+id/viewswitcher"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<!-- Add Two View’s Here -- >
</ViewSwitcher>
You can take reference from this link: http://abhiandroid.com/ui/viewswitcher
Try out as below:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="78dp"
android:onclick="start"
android:text="#string/HTI" />
In your main activity :
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(this, ActivityStore.class);
startActivity(i);
}
});
Here is your ActivityStore Class code:
public class ActivityStore extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store);
}
}
Also add the activity into your mainfest file.
<activity
android:name=".ActivityStore"
android:label="#string/app_name"/ >
You can't add the launch of an Intent inside the onclick parameter in XML. You have to do it by code.
In your code:
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(this, ActivityStore.class);
startActivity(i);
}
});
And in the OnCreate of the ActivityStore class, put this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store);
}
NOTE: I supposed that yous activity_store class is called ActivityStore
You need to check on Android documentation :
Activity
OnClickListener
Intent
http://developer.android.com/training/index.html
Good luck.
Try this,
Statically include XML layouts inside other XML layouts. use include. Add the below code in your activity_store.xml
<include layout="#layout/activity_home"/>
Sure you will get solution.
A simple way would be to create an Activity with another xml attached to it and then use intent.

How to enable a button if the condition is true in android?

I have 4 xml named a1,a2 and a3 then I also have a main xml. In a1 there's an editText and an ok button, in a2 there's an ok button which is "disabled". Now, what I want to happen is when the data entered in a1 is correct, it will proceed to a2 then the "disabled" button there should now be "enabled". Now what happened on my program is, it's already running except for the button in a2 is getting enabled but it will return immediately into being disabled. How can I prevent it from getting disabled after it's been enabled? I'm new in android so please explain it as simple as it can be. Thanks in advance.
here's my code
Main Activity.java
package com.example.myact1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnClose = (Button) findViewById(R.id.btnExit);
btnClose.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
finish();
System.exit(0);
}
});
Button page1 = (Button) findViewById(R.id.btn1);
page1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), a1.class);
startActivityForResult(myIntent, 0);
}
});
}
}
MainActivity.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"
tools:context=".MainActivity" >
<Button
android:id="#+id/btnExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="45dp"
android:text="Exit" />
<Button
android:id="#+id/btn1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/btnExit"
android:layout_alignLeft="#+id/btnExit"
android:layout_marginBottom="30dp"
android:text="enter" />
a1 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:orientation="vertical" >
<Button
android:id="#+id/btna1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="155dp"
android:text="Enter" />
<EditText
android:id="#+id/eta1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/btna1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="52dp"
android:ems="10" >
<requestFocus />
</EditText>
a1.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a1);
Button page1 = (Button) findViewById(R.id.btna1);
page1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
EditText a1et = (EditText) findViewById(R.id.eta1);
String a1 = a1et.getText().toString();
if (a1.equalsIgnoreCase("abcde")) {
Toast.makeText(getApplicationContext(), "correct", Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(view.getContext(), a2.class);
startActivityForResult(myIntent, 0);
//enables the button in a2
setContentView(R.layout.a2);
Button stage2 = (Button) findViewById(R.id.btna2);
stage2.setClickable(true);
stage2.setEnabled(true);
} else {
Toast.makeText(getApplicationContext(), "wrong", Toast.LENGTH_SHORT).show();
}
}
});
}
here's the code of my a2.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a2);
}
}
a2.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:orientation="vertical" >
<Button
android:id="#+id/btna2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="102dp"
android:clickable="false"
android:enabled="false"
android:text="enter" />
Also add
stage2.setEnabled(true);
Right below:
Button stage2 = (Button) findViewById(R.id.btna2);
stage2.setClickable(true);
EDIT After seeing all of your code, here are two corrections I did to your code so that it will work:
Remove this part of the code in a1.java:
//enables the button in a2
setContentView(R.layout.a2);
Button stage2 = (Button) findViewById(R.id.btna2);
stage2.setClickable(true);
stage2.setEnabled(true);
Remember that we decided to use new activity and not to change the contentView of the current one? Because you had this code the button appeared enabled for a short while before it became disabled once more.
Change the code of a2.java#onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a2);
Button stage2 = (Button) findViewById(R.id.btna2);
stage2.setEnabled(true);
}
Here you were not enabling the button and that was your problem. Rendering a2 Activity you overwrite the view you already rendered in a1 Activity with a new one. One that has the button disabled once more. I correct that with this code.

Categories

Resources