I have a button that calls a dialog. From that button i have 8 buttons: 1,2,3,4,5,6,7, and cancel. These buttons will be used to change the text of the button. The thing is that it doesn't do anything if i set the text inside the dialog.
buttonDefineHits = (Button) rowView.findViewById(R.id.button_define_hits);
buttonDefineHits.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Dialog 1-7 i x para definir los holes
setDialogSetHits();
}
});
.
private void setDialogSetHits(){
final Dialog dialogConfirmPlayers = new Dialog (activity);
dialogConfirmPlayers.setCancelable(false);
dialogConfirmPlayers.setContentView(R.layout.dialog_set_hits);
Button button1Hit = (Button) dialogConfirmPlayers.findViewById(R.id.button_1_hit);
button1Hit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Dialog 1-7 i x para definir los holes
buttonDefineHits.setText("1");
dialogConfirmPlayers.cancel();
}
});
dialogConfirmPlayers.show();
}
You can set the Text of a Button that is defined in a activity from the dialogBox. I guess Aniruddha is wrong in his comment. I mean yes the user cannot have a "Iteration" with the Activity's UI elements as long as a Dialog is shown over it, But programatically you can change the Text property of the Button in your activity. To confirm, this is what I tried:
Created a Dialog on the click event of ImageView.
From the Dialog Button's Click listener, I changed the Text of a editText in the Activity.
Similarly, you should also be able to set the text of the button from the dialog button's click listener.
I think you should remove the 7 buttons from your dialog, and for testing purpose just have one button on it. Then handle the click event on this button and try n set the Activity button's Text. This should work like charm.
Then later you can integrate your 7-buttons.
Here is the working example, you need to modify it accordingly
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" >
<TextView
android:layout_width="wrap_content"
android:id="#+id/tv1"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/buttonMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv1"
android:layout_below="#+id/tv1"
android:layout_marginTop="44dp"
android:text="Button" />
</RelativeLayout>
dialog_view.xml
<?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="OK" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
TextView t;
Button bMain;
Dialog d;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t = (TextView) findViewById(R.id.tv1);
bMain = (Button) findViewById(R.id.buttonMain);
bMain.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
d = new Dialog(MainActivity.this);
d.setTitle("Hello Android..!");
d.setContentView(R.layout.dialog_view);
Button bOK = (Button) d.findViewById(R.id.button1);
Button bCancel = (Button) d.findViewById(R.id.button2);
d.show();
bOK.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
t.setText("OK");
bMain.setText("Changed the text");
d.cancel();
}
});
}
});
}
#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;
}
}
Related
i want to have two relativelayout in first relativelayout have map and in second relativelayout i have the list..,i want on starting only layout with map will be visible on screen with a button,,when i click on button then layout with listview get open from right side with new new button on the top of it,,and prevoius button get hide.and screen get divided with two parts with different layouts..i have done some thing but from starting onward m getting half half screen.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/ListView_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1" >
<RelativeLayout
android:id="#+id/rl_ListView1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5" >
<Button
android:id="#+id/getdirection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Directions" />
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" >
</fragment>
</RelativeLayout>
<RelativeLayout
android:id="#+id/rl_ListView2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:visibility="invisible" >
<Button
android:id="#+id/hide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Directions" />
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:visibility="invisible" />
</RelativeLayout>
</LinearLayout>
MainActivity
show = (TextView)findViewById(R.id.getdirection);
show1 = (TextView)findViewById(R.id.hide);
rt = (RelativeLayout)findViewById(R.id.rl_ListView2);
show.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(rt.getVisibility()==View.INVISIBLE)
{
rt.setVisibility(View.VISIBLE);
}
show.setVisibility(View.INVISIBLE);
}
});
show1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(rt.getVisibility()==View.VISIBLE)
{
rt.setVisibility(View.INVISIBLE);
}
show1.setVisibility(View.INVISIBLE);
}
});
Try this:
You are getting layout1 in half screen from starting due to weight property. You can try not giving weight in starting and give it programatically on button click.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
button1.setVisibility(View.GONE); //hide old button
layout2.setVisibility(View.VISIBLE); //show layout2
//set Relativelayout 1 to half screen
RelativaLayout.LayoutParams params = layout1.getLayoutParams();
params.weight = 0.5;
layout1.setLayoutParams(params);
}
});
Hope this helps.
Set visibility as Gone instead of Invisible
button.setOnClickListener(this);
public void onClick(View v) {
layoutid.setVisibility(View.GONE);
}
You can try with isShown() as suggested by Amiya
<b>
button2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
if(button1.isShown()) {
// Your_Staff
}
else{
// Your_Staff
}
}
});
</b>
From my view you did not set the orientation in the LinearLayout to be vertical for the two RelativeLayouts. I also suggests you put the map and Button in a FrameLayout as well instead of RelativeLayout.
I have a button with id "ok" in my dialog.
Partial - Dialog Layout:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="8dp"
android:layout_below="#+id/licenseTypeLayout">
<Button
android:id="#+id/ok"
style="#style/agreement_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Ok"/>
</LinearLayout>
In activity:
Dialog dialog;
#OptionsItem(R.id.action_about)
boolean displayPopup() {
dialog = new Dialog(this);
dialog.setContentView(R.layout.about_dialog);
dialog.setTitle(R.string.app_name);
dialog.show();
Button btn=(Button)findViewById(R.id.ok);
//btn remains empty
return true;
}
How would I write onClick() event for this button?
To initialize the Button, you need to use the Dialog object
Button btn = (Button) dialog.findViewById(R.id.ok);
Then set OnClickListener to the Button using setOnClickListener() as follows...
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//write your code here
}
});
I would guess you make the method:
public void closeDialog(View v){
// Do what you want to do here (event handler)
}
?
So I have a 2 buttons in a layout
like so:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:onClick="onClick">
<Button
android:id="#+id/btnEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Edit Spot" />
<Button android:id="#+id/btnGo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Google Spot"
android:layout_weight="1"/>
</LinearLayout>
Then in the onCreate for the activity for this I have:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_spot);
// save button
ButtonEdit = (Button)findViewById(R.id.btnEdit);
ButtonGo = (Button) findViewById(R.id.btnGo);
// getting product details from intent
Intent i = getIntent();
// getting product id (pid) from intent
pid = i.getStringExtra(TAG_PID);
// Getting complete product details in background thread
new GetProductDetails().execute();
// save button click event
ButtonEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Edit button pressed", Toast.LENGTH_LONG).show();
}
});
// Go button click event
ButtonGo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Go button pressed!", Toast.LENGTH_LONG).show();
}
});
}
But for some reason when I tap the buttons nothing happens. I've looked around at others asking the question and tried those but still nothing. Can someone please help me figure out why it isn't working?
Thank you,
Tyler
You have to add the onClick properties in the Button's definition, not in the LinearLayout.
Even if you want to reuse the same method called onClick you can set a tag for each button, and do a switch for each tag. For instance:
In your layout:
android:tag="1"
In your code:
public void onClick(View v) {
String tag = (String) v.getTag();
switch (Integer.parseInt(tag)) {
case 1: // First button
...
break;
case 2: // Second button
...
break;
}
}
Remove onClick from your LinearLayout. Right now you have nested onClickListeners. The LinearLayout is intercepting the event and likely not passing it down.
edit: somebody is blindly downvoting all these answers for some reason. Post a comment explaining why this is wrong.
Please make your Button(s) clickable:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:onClick="onClick">
<Button
android:id="#+id/btnEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Edit Spot"
android:clickable:true />
<Button android:id="#+id/btnGo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Google Spot"
android:layout_weight="1"
android:clickable:true/>
</LinearLayout>
Also implement the onClick() method in your Activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_spot);
// save button
ButtonEdit = (Button)findViewById(R.id.btnEdit);
ButtonGo = (Button) findViewById(R.id.btnGo);
// getting product details from intent
Intent i = getIntent();
// getting product id (pid) from intent
pid = i.getStringExtra(TAG_PID);
// Getting complete product details in background thread
new GetProductDetails().execute();
// save button click event
ButtonEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Edit button pressed", Toast.LENGTH_LONG).show();
}
});
// Go button click event
ButtonGo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Go button pressed!", Toast.LENGTH_LONG).show();
}
});
}
public void onCLick(View v)
{
//Your Implementation
}
I hope this helps.
I need to disable the click-event for a button in Android. Just as a sample, I have tried doing the following. I have taken a TextView named it (entered a text) as Name. The condition checks if, TextView is empty button and clickable should be set to false. However this does not happen when the Toast is printed. Can somemone tell me the reason. Also if the text field is not empty I want to reset the clickable event for button as true.
Java File:
public class ButtonclickabkeActivity extends Activity {
TextView tv;
Button btn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView1);
tv.setText("Name");
btn = (Button) findViewById(R.id.button1);
if (tv.getText().toString().length() != 0) {
btn.setClickable(false);
Toast.makeText(getApplicationContext(), "" + tv.getText().toString().length(), Toast.LENGTH_LONG).show();
} else {
btn.setClickable(true);
}
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Button clicked", Toast.LENGTH_LONG).show();
}
});
}
}
XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"/>
<TextView
android:text="TextView"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="Button"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Use
btn.setEnable(false);
instead of
btn.setClickable(false);
User 370305 is correct. .setEnable is what your looking for. Or you could use android:clickable in the layout XML file.
In my case
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
view.setEnabled(false);
}
});
here I have a custom dialog with background 2 ImageButton inside it.
the problem is, when I try to set onclick listener to that buttons, the program will return NullPointerException. I don't know why is this happen. how to assign operation to button inside dialog anyway??
pause menu xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:id="#+id/linearLayout1" android:layout_width="wrap_content" android:background="#drawable/pause_menu_cropped" android:layout_gravity="center" android:gravity="center|center_horizontal">
<TableLayout android:layout_width="wrap_content" android:id="#+id/tableLayout1" android:layout_height="wrap_content">
<ImageButton android:src="#drawable/pause_button_option" android:layout_width="wrap_content" android:background="#drawable/pause_button_option" android:layout_height="wrap_content" android:id="#+id/btn_pause_option"></ImageButton>
<ImageButton android:src="#drawable/pause_button_quit" android:layout_width="wrap_content" android:background="#drawable/pause_button_quit" android:layout_height="wrap_content" android:id="#+id/btn_pause_quit"></ImageButton>
</TableLayout>
</LinearLayout>
dialog code
Dialog pauseMenu = new Dialog(this, R.style.NewDialog);
pauseMenu.setContentView(R.layout.pause_menu);
ImageButton quit = (ImageButton)findViewById(R.id.btn_pause_quit);
quit.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
TestActivity.this.finish();
}
});
return pauseMenu;
the code is returning error in line
quit.setOnClickListener();
ImageButton quit =
(ImageButton)findViewById(R.id.btn_pause_quit);
should be
ImageButton quit = (ImageButton)pauseMenu.findViewById(R.id.btn_pause_quit);
This happens because findViewById is invoked for the activity, and it doesn't have btn_pause_quit button in it's layout. But your dialog has.
U can use this custom dialog and onclicklistener..
public class CustomizeDialog extends Dialog implements OnClickListener {
Button okButton;
public CustomizeDialog(Context context) {
super(context);
/** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
requestWindowFeature(Window.FEATURE_NO_TITLE);
/** Design the dialog in main.xml file */
setContentView(R.layout.main);
okButton = (Button) findViewById(R.id.OkButton);
okButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
/** When OK Button is clicked, dismiss the dialog */
if (v == okButton)
dismiss();
}
}
I think your onClickListener should be DialogInterface.OnClickListener