Android Button Disable and enable - android

Hello I have an android application i want to create something like a lock button to the prevent error click on any button in application i use this method and it work great in disable all button i like but i can not re-enable it again after this all i need is that when i press lock button again it re-enable button2 and button 3 again
This is the activity code
import android.support.v7.app.ActionBarActivity;
import android.app.ActionBar;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
public class AboutActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
addListenerOnButton();
}
Button button;
Button button2;
Button button3;
private void addListenerOnButton() {
// TODO Auto-generated method stub
button = (Button) findViewById(R.id.lock);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button2.setEnabled(false); //button which u want to disable
button3.setEnabled(false); //button which u want to disable
}
});
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.about, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
This is layout code
<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="com.lock.AboutActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/lock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/button2"
android:layout_below="#+id/button2"
android:layout_marginTop="66dp"
android:enabled="true"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/lock"
android:layout_centerVertical="true"
android:enabled="true"
android:text="Button" />
</RelativeLayout>

You can use isEnabled() and use "not" operator to lock and unlock. If it's not enabled, it'll enable and vice versa.
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button2.setEnabled(!button2.isEnabled()); //button which u want to disable
button3.setEnabled(!button3.isEnabled()); //button which u want to disable
}
});

Related

Changing text in textView

I am trying to change the text in one of the textview by calling it in a function that is assigned to a button.. However it is not quite working as I hoped. I was wondering if some one could point out why isn't it working?
Here is the "MainActivity.java" file
package com.nblsoft.myapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
String stri = new String("Hellow");
public void game(View view) {
TextView textv = (TextView) findViewById(R.id.textView2);
textv.setText(stri);
}
}
and the "activity_main.xml" file
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/Welcome"
android:id="#+id/textView"
android:gravity="center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/lower_text"
android:id="#+id/textView2"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:inputType="number"
android:text="#string/guess"
android:textSize="160dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="game"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ok"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
you want call game when click in EditText?! you put android:onClick="game" in EditText
if want call when click in Button you must add android:onClick="game" to your Button in activity_main.xml file like
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ok"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="game" />
or you can use this in MainActivity.java instead use android:onClick="game" in xml file
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
game(arg0);
}
});
}
- remove android:onClick="game" from EditText
package com.nblsoft.myapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textv = (TextView) findViewById(R.id.textView2);
String stri = new String("Hellow");
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textv.setText(stri);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
This should be working.By the way.Don't rush into android like this...you've got to learn a few things about how it works before you can play with it.Try to check the documentation and play with all that android offers.It's actually fun.It was for me and it still is as I've got to learn a lot ,still.
you call the method game() when you click the edittext "editText", and this will change the textview. If you want to change the value of the textview by clicking button "button", i think you just need to move the game() method to the Button field:
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ok"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="game" />

i need to make a button that opens a custom dialogbox

I'm creating an android app for my school project.
I created the interface normaly.
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:background="#drawable/mappic"
android:layout_above="#+id/zoomControls"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ZoomControls
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/zoomControls"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/imageView"
android:layout_alignEnd="#+id/imageView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Obtenir le PCC"
android:id="#+id/button"
android:layout_alignBottom="#+id/zoomControls"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
now I want to make it when I click on the button : Obtenir le PCC this dialog box opens up :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:backgroundTintMode="multiply"
android:backgroundTint="#ff7518ff">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/Suivantbtn"
android:layout_alignLeft="#+id/Suivantbtn"
android:layout_alignStart="#+id/Suivantbtn" />
<TextView
android:id="#+id/textdp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Taper le poit de départ:"
android:textColor="#ff111124"
android:layout_alignBottom="#+id/Suivantbtn"
android:theme="#style/AppTheme"
android:textSize="30dp"
android:layout_below="#+id/image"
android:layout_marginTop="50dp"
android:layout_marginBottom="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="28dp"
android:layout_marginLeft="28dp" />/>
<Button
android:id="#+id/Suivantbtn"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="Suivant "
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_above="#+id/Suivantbtn"
android:layout_marginBottom="56dp"
android:layout_alignLeft="#+id/textdp"
android:layout_alignStart="#+id/textdp"
android:layout_alignRight="#+id/textdp"
android:layout_alignEnd="#+id/textdp"
android:textAlignment="center"
android:backgroundTint="#ff9eadff" />
</RelativeLayout>
and when I click on Suivant ,, the dialog box goes and another dialogbox appears :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:backgroundTintMode="multiply"
android:backgroundTint="#ff7518ff">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/Suivantbtn2"
android:layout_alignLeft="#+id/Suivantbtn2"
android:layout_alignStart="#+id/Suivantbtn2" />
<TextView
android:id="#+id/textar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Taper le poit d'arrivée:"
android:textColor="#ff111124"
android:layout_alignBottom="#+id/Suivantbtn2"
android:theme="#style/AppTheme"
android:textSize="30dp"
android:layout_below="#+id/image"
android:layout_marginTop="50dp"
android:layout_marginBottom="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="28dp"
android:layout_marginLeft="28dp" />/>
<Button
android:id="#+id/Suivantbtn2"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="Suivant "
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_above="#+id/Suivantbtn2"
android:layout_marginBottom="56dp"
android:layout_alignLeft="#+id/textar"
android:layout_alignStart="#+id/textar"
android:layout_alignRight="#+id/textar"
android:layout_alignEnd="#+id/textar"
android:textAlignment="center"
android:backgroundTint="#ff9eadff" />
</RelativeLayout>
the java code :
package artofdev.org.admaps;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Context context = getApplicationContext();
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog);
Button button2 = (Button) dialog.findViewById(R.id.Suivantbtn);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog2 = new Dialog(context);
dialog2.setContentView(R.layout.dialog2);
Button button3 = (Button)
dialog2.findViewById(R.id.Suivantbtn2);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog2.dismiss();
}
});
dialog.dismiss();
dialog2.show();
}
});
dialog.show();
}
});
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And when this appears and I type in and click on next. This dialog box dismisses and another msg box I'll create later shows a msg.
how to do it ?
Here is a fully working code for you:
Context context = getAplicationContext();
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.your_dialog_layout);
Button button2 = (Button) dialog.findViewById(R.id.Suivantbtn);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog2 = new Dialog(context);
dialog2.setContentView(R.layout.your_second_dialog_layout);
Button button3 = (Button) dialog2.findViewById(R.id.Suivantbtn2);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog2.dismiss();
}
});
dialog.dismiss();
dialog2.show();
}
});
dialog.show();
}
});
Hope my answer helped!
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
#Override
//On click function
public void onClick(View view) {
//do something - for example open the dialog box you want
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_alert_dialogue);
dialog.show();
}
});
You should add onClickListener to your buttons. For example (this code is for your Activity class):
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
#Override
//On click function
public void onClick(View view) {
//do something - for example open the dialog box you want
}
});

App force close when changing items in StringArray, Android?

right now I'm working on a app which changes items in StringArray on button clicks. I've implemented left and right buttons, when I click right button the item changes by right, when I press left the item changes to left. Up to here everything is fine, but when I click left button when the item is on Steve Jobs the app force closes. Any idea why this is happening?
Strings.xml:
<string name="sj">Steve Jobs</string>
<string name="mz">Mark Zuckerberg</string>
<string name="bg">Bill Gates</string>
<string name="lp">Larry Page</string>
<string-array name="gl">
<item>#string/sj</item>
<item>#string/mz</item>
<item>#string/bg</item>
<item>#string/lp</item>
</string-array>
MainActivity.java:
package com.example.theatremode;
import android.support.v7.app.ActionBarActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements OnClickListener {
int counter = 0;
TextView tv1;
Button button1;
Button button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv1);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
Resources res = getResources();
final String[] list = res.getStringArray(R.array.gl); // get the array
((TextView) findViewById(R.id.tv1)).setText(list[counter]); // set the
// initial
// message.
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tx = (TextView) findViewById(R.id.tv1);
counter++;
if (counter >= list.length)
counter = 0;
tx.setText(list[counter]); // set the new message.
}
});
Resources res2 = getResources();
final String[] list2 = res2.getStringArray(R.array.gl); // get the array
((TextView) findViewById(R.id.tv1)).setText(list2[counter]);
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tx2 = (TextView) findViewById(R.id.tv1);
counter--;
if (counter >= list2.length)
counter = 0;
tx2.setText(list2[counter]); // set the new message.
}
});
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
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="com.example.theatremode.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:id="#+id/tv1"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_alignLeft="#+id/tv1"
android:text="Left" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="48dp"
android:layout_marginRight="14dp"
android:text="Right" />
Looks like you have a IndexOutOfBoundsException.
It's not obvious exactly what's going on here, but this might fix it:
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tx2 = (TextView) findViewById(R.id.tv1);
counter--;
//if (counter >= list2.length)
//counter = 0;
if (counter < 0)
counter = list2.length - 1;
tx2.setText(list2[counter]); // set the new message.
}
});

android number picker default design changes in jelly bean and ice-cream sandwitch

i've created an android application which displays a number picker, it all works fine...but the problem is with the design....when i run the application in gingerbread the number picker looks fine good....but when i run the same stuff in ice-cream sandwich and jelly bean the number picker design is been altered just like as shown below.
can anyone please tell me how to retain the default number-picker design that is in gingerbread in jelly bean
when runs in ice-cream sandwich and jelly bean
when runs in ginger-bread
i'm using a custom dialog box within which the number picker is placed, the code is as given below
import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.NumberPicker;
public class QuantityChangeDialog extends Dialog implements android.view.View.OnClickListener {
public Activity c;
public Dialog d;
public Button save, cancel;
NumberPicker np;
public QuantityChangeDialog(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
setContentView(R.layout.selecteditem_dialog);
save = (Button) findViewById(R.id.btn_save);
cancel = (Button) findViewById(R.id.btn_cancel);
save.setOnClickListener(this);
cancel.setOnClickListener(this);
np = (NumberPicker) findViewById(R.id.qntypicker);
np.setMaxValue(120);
np.setMinValue(1);
np.setValue(3);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_save:
c.finish();
break;
case R.id.btn_cancel:
dismiss();
break;
default:
break;
}
dismiss();
}
}
Quoting from docs
If the current theme is derived from Theme the widget presents the current value as an editable input field with an increment button above and a decrement button below. Long pressing the buttons allows for a quick change of the current value. Tapping on the input field allows to type in a desired value.
You need to set your theme that is derieved from Theme like fro example Theme.NoTitleBar.Fullscreen
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:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Open" />
</RelativeLayout>
dialog.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:theme = "#style/cust_dialog"
android:layout_height="fill_parent" >
<NumberPicker
android:id="#+id/numberPicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/numberPicker1"
android:layout_marginLeft="20dp"
android:layout_marginTop="98dp"
android:layout_toRightOf="#+id/numberPicker1"
android:text="Cancel" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_marginRight="16dp"
android:layout_toLeftOf="#+id/numberPicker1"
android:text="Set" />
</RelativeLayout>
Then to display custom dialog
public class MainActivity extends Activity implements NumberPicker.OnValueChangeListener
{
private TextView tv;
static Dialog d ;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
tv.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
tv.setTextColor(Color.RED);
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
// set to normal color
tv.setTextColor(0);
}
return true;
}
});
Button b = (Button) findViewById(R.id.button11);
b.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
show();
}
});
}
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
Log.i("value is",""+newVal);
}
public void show()
{
final Dialog d=new Dialog(this,R.style.cust_dialog);
d.setTitle("NumberPicker");
d.setContentView(R.layout.dialog);
Button b1 = (Button) d.findViewById(R.id.button1);
Button b2 = (Button) d.findViewById(R.id.button2);
final NumberPicker np = (NumberPicker) d.findViewById(R.id.numberPicker1);
np.setMaxValue(100);
np.setMinValue(0);
np.setWrapSelectorWheel(false);
np.setOnValueChangedListener(this);
b1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
tv.setText(String.valueOf(np.getValue()));
d.dismiss();
}
});
b2.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
d.dismiss();
}
});
d.show();
}
}
Styles.xml
</style>
<style name="cust_dialog" parent="#android:style/Theme.NoTitleBar.Fullscreen">
</style>
Snap Shot
You can just add this attribute into your NumberPicker
android:theme="#android:style/Theme.Dialog"
E.g.
<NumberPicker android:theme="#android:style/Theme.Dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
This will limit the impact to just the number picker widget, not the entire activity page.

2 button OnClick event on Popup Window in Android

I have 2 buttons in my xml file, onclicklistener open new popup.why this eror? can help me??
My Java code is as follows:
Public class Bab1_b1a extends Activity {
final Context context = this;
private Button hans, logemann;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bab1_b1a);
hans = (Button) findViewById(R.id.hans);
logemann = (Button) findViewById(R.id.logemann);
hans.setOnClickListener(myhandler1);
logemann.setOnClickListener(myhandler2);
}
View.OnClickListener myhandler1 = new View.OnClickListener() {
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.activity_popup_hans);
dialog.setTitle("Hans Kelsen");
TextView text1 = (TextView) dialog.findViewById(R.id.textView1);
text1.setText("Negara ialah");
ImageView image = (ImageView) dialog.findViewById(R.id.imageView1);
image.setImageResource(R.drawable.hans_kelsen);
Button dialogButton = (Button) dialog.findViewById(R.id.back_hans);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
};
View.OnClickListener myhandler2 = new View.OnClickListener() {
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.popup_logemann);
dialog.setTitle("Logemann");`enter code here`
TextView text1 = (TextView) dialog.findViewById(R.id.textView1);
text1.setText("Negara ialah.");
ImageView image = (ImageView) dialog.findViewById(R.id.imageView1);
image.setImageResource(R.drawable.logemann);
Button dialogButton = (Button) dialog.findViewById(R.id.back_logemann);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
};
};
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:background="#drawable/back"
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=".Bab1_b1a" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/nav_back"
android:contentDescription="#string/skx" />
<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="#string/h_neg"
android:textSize="13sp" />
<Button
android:id="#+id/hans"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hans_kelsen"
android:textSize="15sp" />
<Button
android:id="#+id/logemann"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/logemann" />
</LinearLayout>
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/nav_forw"
android:contentDescription="#string/skx" />
</LinearLayout>
Can anyone help me know the reason for this problem and how do I solve it. I am a newbie to Android.
Thanks
try this
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt_1 = (Button) findViewById(R.id.bt1);
bt_1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.activity_popup);
dialog.setTitle("Hans Kelsen");
//Her add your textView and ImageView if you want
Button dialogButton = (Button) dialog.findViewById(R.id.bt1_popUP);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
// Same thing for bt_2
}
#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;
}
}
Your Main Activity 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/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BT 1" />
<Button
android:id="#+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/bt1"
android:layout_marginTop="40dp"
android:text="BT 2" />
</RelativeLayout>
Your POP-UP 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="200dp"
android:background="#FFF123"
android:gravity="center" >
<Button
android:id="#+id/bt1_popUP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close" />
</RelativeLayout>
enjoy :)

Categories

Resources