I want to create a custom keyboard inside a custom dialog.
It has a Textview on top and 12 buttons bellow (it's a numerical keyboard)
What I want is: when the buttons are pressed they update the Textview values.
I don't know the right place to put the "OnClickListener" and how to update the Textview...
Here is the keyboard layout:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView
android:id="#+id/valor"
android:layout_width="300dp"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<Button
android:id="#+id/btn7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7"/>
<Button
android:id="#+id/btn8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8"/>
<Button
android:id="#+id/btn9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9"/>
</TableRow>
<TableRow>
<Button
android:id="#+id/btn4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4"/>
<Button
android:id="#+id/btn5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5"/>
<Button
android:id="#+id/btn6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6"/>
</TableRow>
<TableRow>
<Button
android:id="#+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"/>
<Button
android:id="#+id/btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2"/>
<Button
android:id="#+id/btn3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3"/>
</TableRow>
<TableRow>
<Button
android:id="#+id/btn0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0"/>
<Button
android:id="#+id/btnP"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="."/>
<Button
android:id="#+id/btnOk"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Ok"/>
</TableRow>
And here is the MainActivity:
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button teclado = (Button)findViewById(R.id.teclado);
teclado.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
showCustomDialog();
}
});
}
public void showCustomDialog()
{
final Dialog dialog = new Dialog(this);
dialog.getWindow();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.teclado);
dialog.show();
final TextView valor = (TextView) dialog.findViewById(R.id.valor);
Button btn7 = (Button)dialog.findViewById(R.id.teclado);
btn7.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
valor.setText("Test");
}
});
}
}
Thanks in advance!!
Sorry, I found this implementation strange.
I would suggest using DialogFragment, and you can encapsulate everything in there.
http://android-developers.blogspot.ca/2012/05/using-dialogfragments.html
You will be able to use the normal setOnClickListener in that fragment.
Button btn7 = (Button)dialog.findViewById(R.id.btn7); **"why R.id.teclado?"**
btn7.setOnClickListener(this)
And your fragment can implement OnClickListener with switch
#Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btn7:
valor.setText("asdf");
break;
case R.id.btn8:
...
break;
....
}
// TODO Auto-generated method stub
}
Ok I want to give some basic idea regarding this. you can modify this according to your requirement.
Give your layout an id. and use layoutinflater to inflate your xml in dialog.
If you want your xml layout in your dialog box you you can do it like this:
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.your_layout_id_here, (ViewGroup) getCurrentFocus());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);
builder.show();
Put above code in Button's onclicklistener. And check Button's view through Switch case like case 1: "this button id" then insert this number in Text view.
Hope this help you
R.id.teclado is not in the dialog layout
Change:
Button btn7 = (Button)dialog.findViewById(R.id.teclado);
To:
Button btn7 = (Button)dialog.findViewById(R.id.btn7);
Related
I'm very new to this. My buttons weren't responding to clicks. I added android:onClick="onClick" to the xml file. Now i get the error below.
java.lang.IllegalStateException: Could not find a method onClick(View) in the activity class testapp.two.MainActivity for onClick handler on view class android.widget.Button with id 'buttonMinus'
Please help, java code along with the manifest file and main xml are included below.
Thanks.
package testapp.two;
import testapp.two.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
Button okButton, minusButton, plusButton;
TextView textScore, scoreCard;
int score = 95;
private static final String TAG = "GolfScore";
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate started");
okButton = (Button)findViewById(R.id.buttonOK);
minusButton = (Button)findViewById(R.id.buttonMinus);
plusButton = (Button)findViewById(R.id.buttonPlus);
textScore = (TextView)findViewById(R.id.textScore);
scoreCard = (TextView)findViewById(R.id.scoreCard);
//set button listeners
okButton.setOnClickListener(this);
minusButton.setOnClickListener(this);
plusButton.setOnClickListener(this);
textScore.setText(String.valueOf(score));
Log.d(TAG, "onCreate finished");
}//onCreate
#Override
public void onClick(View v) {
Log.d(TAG, "in onCreate");
switch (v.getId()){
case R.id.buttonMinus:
score--;
textScore.setText(String.valueOf(score));
break;
case R.id.buttonPlus:
score++;
textScore.setText(String.valueOf(score));
break;
case R.id.buttonOK:
break;
}//end of switch
}//end of my onclick
}//end of MainActivity
<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:onClick="#+id/buttonMinus, #+id/buttonPlus, #+id/textScore"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="false"
android:layout_centerVertical="false"
android:text="Golf Score App"
android:textAlignment="center" />
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/title"
android:layout_marginTop="14dp"
android:baselineAligned="false"
android:gravity="fill"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonMinus"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginLeft="5dp"
android:clickable="true"
android:onClick="onClick"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="-" />
<Button
android:id="#+id/buttonPlus"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginLeft="0dp"
android:clickable="true"
android:onClick="onClick"
android:layout_weight="1"
android:text="+" />
<TextView
android:id="#+id/textScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="92"
android:textSize="20sp" />
<Button
android:id="#+id/buttonOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginLeft="0dp"
android:layout_marginRight="33dp"
android:clickable="true"
android:onClick="onClick"
android:layout_weight="1"
android:text="Ok"
android:textSize="15sp" />
</LinearLayout>
<TextView
android:id="#+id/scoreCard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/LinearLayout1"
android:layout_marginTop="22dp"
android:text="Score card info goes here" />
</RelativeLayout>
As 0xDEADC0DE says, you don't need the OnClickListener if you use the onClick attribute in your XML and vice versa.
The reason why the onClick(View v) method can't be found is the Overrideannotation. It depends to the OnClickListener this way. There is a "free" method without annotation needed that the method can be found if the onClick tag is used.
Now you have two options: get rid of the OnClicklistener and the association to the buttons or the onClick in your XML.
Edit:
Remove the onClick attributes from the RelativeLayout and the Buttons in your XML. In addition, remove clickable from your Buttons. If you set clickable = true, the View will get an empty OnClickListener automatically. This means your OnClickListener in your Activity will not be called.
Just remove android:onClick="onClick" from your XML.
Note that it is preferrable to make a single OnClickListener for each button rather than one big OnClickListener that has to check the id to decide what to do.
Hi
I am new in android.
I have many Buttons in view in android and i want to show them part by part by clicking another button.
Set button visibility to GONE (button will be completely "removed" -- the buttons space will be available for another widgets) or INVISIBLE (button will became "transparent" -- its space will not be available for another widgets):
View b = findViewById(R.id.button);
b.setVisibility(View.GONE);
or in xml:
<Button ... android:visibility="gone"/>
Ref By: LINK
use android:visibility="visible" or android:visibility="gone" or android:visibility="invisible"
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="91dp"
android:layout_toLeftOf="#+id/textView1"
android:text="Button"
android:visibility="visible" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_alignLeft="#+id/textView1"
android:text="Button"
android:visibility="invisible" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_toRightOf="#+id/button2"
android:text="Button"
android:visibility="invisible" />
let say you have 2 buttons : in xml write this:Make button 2 invisible like below
<Button android:id="#+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 1"/>
<Button android:id="#+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="button 2"
android:layout_below="#+id/btn1"
android:visibility="invisible"/>
In your oncreate method write this: when you click on button1 , button2 will appear.
Button btn1 = (Button)findViewById(R.id.btn1);
final Button btn2 = (Button)findViewById(R.id.btn2);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn2.setVisibility(View.VISIBLE);
}
});
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="91dp"
android:layout_toLeftOf="#+id/textView1"
android:text="Button"
android:visibility="gone" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_alignLeft="#+id/textView1"
android:text="Button"
android:visibility="gone" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_toRightOf="#+id/button2"
android:text="Button"
android:visibility="gone" />
First make the above xml layout having all your buttons and set them to GONE because the buttons will not occupy space in the layout by default so saves you space and time at initialization.
Now in your code on click event just set the visibility of these buttons to VISIBLE.
Steps for changing the visibility
HashMap map=new HashMap();
map.put(R.id.button1,new Integer[]{R.id.button2,R.id.button3,R.id.button4});
public void onClick(View v){
Integer[] buttonsToShow=map.get(R.id.v.getId());
if(buttonsToShow!=null)
for(int button:buttonsToShow){
findViewById(button).setVisibility(View.VISIBLE);
}
}
I wrote following code and solved my problem. maybe it might be useful to anyone
next = (Button)findViewById(R.id.next);
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
if(i<movie_list.length-3)
{
movie_list[i].setVisibility(View.GONE);
movie_list[i+3].setVisibility(View.VISIBLE);
i++;
}else{
Toast.makeText(getApplicationContext(), "That is the end of the buttons ", Toast.LENGTH_SHORT).show();
}
}
});
I have My Table row like below in my xml layout:
<TableRow
android:layout_marginTop="10dp"
>
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="256dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/titlename"
android:layout_="#+id/playbtn"
android:layout_marginTop="4dp"
/>
<Button
android:id="#+id/playbtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:background="#drawable/play"
android:layout_marginBottom="3dp"/>
<Button
android:id="#+id/pausebtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_toTopOf="#+id/playbtn"
android:layout_alignParentBottom="true"
android:background="#drawable/pause"
android:layout_marginBottom="3dp"/>
</TableRow>
and my output is as below,
my requirement is to show play pause buttons at the same position in my layout?
Could any one help?
Use FrameLayout and put one button over another
Like this
<FrameLayout ... >
<Button
android:id="#+id/playbtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/play"/>
<Button
android:id="#+id/pausebtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/pause"/>
</FrameLayout>
This will put Pause button over Play Button. Make the necessary button visible and invisible according to your need
Try using this layout. Use combination of LinearLayout and FrameLayout to achieve the desired result.
<TableRow
android:layout_marginTop="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weight="1"
android:layout_alignLeft="#+id/titlename"
android:layout_="#+id/playbtn"
android:layout_marginTop="4dp"
/>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/playbtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/play"/>
<Button
android:id="#+id/pausebtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/pause"/>
</FrameLayout>
</LinearLayout>
</TableRow>
once Try this:
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".8" />
<Button
android:id="#+id/play"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".2"
/>
</TableRow>
Main Activity
private boolean playing = false;
Button play;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play=(Button) findViewById(R.id.play);
play.setBackgroundResource(R.drawable.pause);
play.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(playing){
playing = false;
play.setBackgroundResource(R.drawable.pause);
}
else {
playing = true;
play.setBackgroundResource(R.drawable.play);
}
}
});
}
Why can't you try merge?
Have a glance of this.
http://android-developers.blogspot.in/2009/03/android-layout-tricks-3-optimize-by.html
You don't need to create two buttons for it. You can do it with single. I have created a sample program. I find this to be a cleaner approach.
Layout for MainActivity
<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:orientation="vertical"
android:gravity="center">
<Button
android:id="#+id/buttonPlayPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Here is MainActivity
package in.live.homam.imagebuttonexample;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button buttonPlayPause;
private static final int resourceIdPlay = R.drawable.play;
private static final int resourceIdPause = R.drawable.pause;
private int resourceIdButton = resourceIdPlay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonPlayPause = (Button) findViewById(R.id.buttonPlayPause);
buttonPlayPause.setBackgroundResource(resourceIdButton);
buttonPlayPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(resourceIdButton == resourceIdPlay) {
resourceIdButton = resourceIdPause;
Log.d("Tag", "Playing");
}
else {
resourceIdButton = resourceIdPlay;
Log.d("Tag", "Paused");
}
buttonPlayPause.setBackgroundResource(resourceIdButton);
}
});
}
}
I need to create a dialog that contains a matrix of simple square ImageButtons (like icons), in unknown number, and a Cancel button. The dialog should be inflatable programmatically and scrollable.
I really am confused: should I create a custom layout and apply it to AlertDialog? And how could I intercept the clicks? Should I use PopUpWiew? Should I make it as Activity or not?
And if I create a new Activity... should I use a Runnable?
Please don't provide me a complete source, I just need to understand what is the correct direction to go for this need.
Thank you in advance.
You can use a Dialog and set your layout file to it. A dialog is something like a mini activity which can be called onto an actual activity and dismissed at will.
Let this be the custom layout file - customLayout.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:background="#drawable/bg"
tools:context=".AutoMode" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_above="#+id/relativeLayout2"
android:layout_centerHorizontal="true" >
<Button
android:id="#+id/button1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/blue_gradient"
android:layout_alignRight="#+id/relativeLayout1"
android:layout_alignTop="#+id/relativeLayout1"/>
<Button
android:id="#+id/button2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/black_gradient"
android:layout_alignTop="#+id/relativeLayout1"
android:layout_toRightOf="#+id/button1"/>
<Button
android:id="#+id/button3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:layout_toRightOf="#+id/button2"
android:background="#drawable/red_gradient" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
<Button
android:id="#+id/button4"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/yellow_gradient"
android:layout_alignRight="#+id/relativeLayout2"
android:layout_alignTop="#+id/relativeLayout2" />
<Button
android:id="#+id/button5"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/metallic_gradient"
android:layout_alignTop="#+id/relativeLayout2"
android:layout_toRightOf="#+id/button4" />
<Button
android:id="#+id/button6"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/purple_gradient"
android:layout_alignTop="#+id/relativeLayout2"
android:layout_toRightOf="#+id/button5"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_below="#+id/relativeLayout2"
android:layout_centerHorizontal="true" >
<Button
android:id="#+id/button7"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/green_gradient"
android:layout_alignRight="#+id/relativeLayout3"
android:layout_alignTop="#+id/relativeLayout3" />
<Button
android:id="#+id/button8"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/orange_gradient"
android:layout_alignTop="#+id/relativeLayout3"
android:layout_toRightOf="#+id/button7" />
<Button
android:id="#+id/button9"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/violet_gradient"
android:layout_alignTop="#+id/relativeLayout3"
android:layout_toRightOf="#+id/button8" />
</RelativeLayout>
Here's how you can set this layout to a dialog box in the activity where you want to call it:
final Dialog customDialog = new Dialog(this);
customDialog.setTitle("Matrix");
disclaimer.setContentView(R.layout.customLayout);
Button b1 = (Button) customDialog.findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Do whatever you want when button1 is clicked
}
});
.
.//same for other buttons
.
Button b9 = (Button) customDialog.findViewById(R.id.button9)
b9.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Do whatever you want when button9 is clicked
}
});
Button close = (Button) customDialog.findViewById(R.id.dismiss);
customDialog.show();
close.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
disclaimer.dismiss();
}
});
I have written a code to enter some text using button click events in android. It is not working at all. Not giving any error or exception,i am not getting where it is all going wrong.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//select_btn = (Button) findViewById(R.id.ok);
//home_btn = (Button) findViewById(R.id.home);
et = (EditText) findViewById(R.id.entry);
et.setText("", TextView.BufferType.EDITABLE );
b = new Bundle();
for (int i = 0; i < mybtn.length; i++) {
String btnid = "btn" + i;
int resid = getResources().getIdentifier(btnid, "id",
getPackageName());
mybtn[i] = (Button) findViewById(resid);
mybtn[i].setOnClickListener(this);
}
//tts.speak("hello wolrd",TextToSpeech.QUEUE_FLUSH,null);
et.setText("hello man", TextView.BufferType.EDITABLE );
}
public void onClick(View v)
{
//while(tts.isSpeaking());
et.setText("hello android", TextView.BufferType.EDITABLE );
String s=null;
s = ((Button)v).getText().toString().trim();
//tts.speak("hello google",TextToSpeech.QUEUE_FLUSH,null);
}
here is the main.xml :
<EditText
android:id="#+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:inputType="phone"
android:maxLength="20" />
<TableLayout
android:id="#+id/table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/entry"
android:layout_marginTop="20dp"
android:stretchColumns="*" >
<TableRow>
<Button
android:id="#+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:text="#string/zero"
android:textSize="10pt"
android:visibility="visible" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:text="#string/one"
android:textSize="10pt"
android:visibility="visible" />
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:text="#string/two"
android:textSize="10pt"
android:visibility="visible" />
<Button
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:text="#string/three"
android:textSize="10pt"
android:visibility="visible" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TableRow>
<Button
android:id="#+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:text="#string/four"
android:textSize="10pt"
android:visibility="visible" />
<Button
android:id="#+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:text="#string/five"
android:textSize="10pt"
android:visibility="visible" />
<Button
android:id="#+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:text="#string/six"
android:textSize="10pt"
android:visibility="visible" />
<Button
android:id="#+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:text="#string/seven"
android:textSize="10pt"
android:visibility="visible" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TableRow>
<Button
android:id="#+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:text="#string/eight"
android:textSize="10pt"
android:visibility="visible" />
<Button
android:id="#+id/btn9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:text="#string/nine"
android:textSize="10pt"
android:visibility="visible" />
<Button
android:id="#+id/btn10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:text="#string/delete"
android:textSize="10pt"
android:visibility="visible" />
<Button
android:id="#+id/btn11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:text="#string/finish"
android:textSize="10pt"
android:visibility="visible" />
</TableRow>
</TableLayout>
<View
android:id="#+id/view1"
android:layout_width="fill_parent"
android:layout_height="90dp"
android:layout_below="#id/table" >
</View>
In your code you are passing the activity into the setOnClickListener() method by mybtn[i].setOnClickListener(this) where this is a reference to your activity. The setOnClickListener() expects as a parameter a View.OnClickListener interface. That is any object that implements the View.OnClickListener Interface. There are two ways that you might do this.
1) Create an anonymous object e.g.
mybtn[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
... your code here
}
});
the new OnClickListener() creates an anonymous object, it is anonymous because we do not store a reference to it.
2) Pass in an object that implements the View.OnClickListener(). If you wanted to pass in your activity object to setOnClickListener() you would have to have it declare that it implements View.OnClickListener() and additionally Override the public void onClick(View v) method e.g.
public class YourActvivity extends Activity implements View.OnClickListener {
onCreate() {
}
....etc
//implement the View.OnClickListener interface
#Override
public void on Click(View v) {
...your code here
}
because you have an array of buttons and calling setOnClickListener(this) where this is your activity this one method will be called for each of your buttons, so if you want each button to do something different you would have to compare the view id that was the source of the click event with the button id in your xml, otherwise all buttons will behave in exactly the same way when clicked.
to find out which button was clicked you can check like this
switch(v.getId()) {
case R.id.btn0:
...do button 0 stuff;
break;
case R.id.btn1:
...do button 1 stuff;
break;
...etc
}
the view that is passed into the onClick() callback is the view that the click event originated from and is referenced here simply as v onClick(View v). This second method will work for any object (Class) that declares and implements View.OnClickListener.
*UPDATE*
Oh sorry...
I think you have a problem with your listener registration, try this instead :
b=findViewById(R.id.btnid);
b.setOnClickListener(this);
b.setOnClickListener(new OnClickListener(){
public void onClick(View view) {
et.setText("hello android", TextView.BufferType.EDITABLE );
String s=null;
s = ((Button)v).getText().toString().trim();
}
});
In the layout add:
android:onClick="onClick"
to the Button on which you want to handle click...
You are reading the text of the clicked button to a string variable, but not using it.
This should be more like what you are trying to achieve:
public void onClick(View v)
{
// et.setText("hello android", TextView.BufferType.EDITABLE );
String s=null;
s = ((Button)v).getText().toString().trim();
et.setText(s, TextView.BufferType.EDITABLE );
}
Make sure your activity is like below
public class MyActivity extends Activity implements OnClickListener
{
Button b;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//select_btn = (Button) findViewById(R.id.ok);
//home_btn = (Button) findViewById(R.id.home);
et = (EditText) findViewById(R.id.entry);
et.setText("", TextView.BufferType.EDITABLE );
b=findViewById(R.id.btnid);
b.setOnClickListener(this);
b = new Bundle();
for (int i = 0; i < mybtn.length; i++) {
String btnid = "btn" + i;
int resid = getResources().getIdentifier(btnid, "id",
getPackageName());
mybtn[i] = (Button) findViewById(resid);
mybtn[i].setOnClickListener(this);
}
//tts.speak("hello wolrd",TextToSpeech.QUEUE_FLUSH,null);
et.setText("hello man", TextView.BufferType.EDITABLE );
}
public void onClick(View v)
{
if(v.equals(b))
{
//while(tts.isSpeaking());
et.setText("hello android", TextView.BufferType.EDITABLE );
String s=null;
s = ((Button)v).getText().toString().trim();
//tts.speak("hello google",TextToSpeech.QUEUE_FLUSH,null);
}
}
}
try this
{
............
int[] mybtn = new int[] {R.id.btn1,......R.id.btn11};
for(int i=0; i<mybtn.length; i++) {
Button b = (Button) findViewById(mybtn[i]);
b.setOnClickListener(this);
}
In
onCreate()
method you should have to mention
et.setOnClickListener(this);
Then override the
onClick(View v)
{
//Your code here
}
This will work!