my app have The local variable eror - java

mytest say: The local variable mytest may not have been initialized
final OnClickListener mytest = new OnClickListener() {
#Override
public void onClick(View arg0) {
Button mybtn = (Button)findViewById(R.id.button1);
mybtn.setOnClickListener(mytest);
}

Can you explain what your Code should do? Because this way of listener implementation can't be work. If you want to add an ClickListener to your button, than use this here:
Button mybtn = (Button) findViewById(R.id.button1);
mybtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Button clicked
}
});

Related

how can I click a button by another button without displaying it?

I want to send the data of a button1 by clicking another button2 is that possible in android, without showing button1 to the user?
You can programmatically "click" a button by calling its callOnClick method.
See this for reference.
You can do that either using View.performClick() or View.callOnClick().
But you should revisit your code. Is the code inside the buttons can be refactored to methods? Is the code can be reused can recall in another button?
What you're code now probably something like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
buttonA.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do process when button A clicked.
}
});
buttonB.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do process when button B clicked.
}
});
...
}
You can refactor the inside of each of your buttons to methods, something like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
buttonA.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do process when button A clicked.
processButtonA();
}
});
buttonB.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do process when button B clicked.
processButtonB();
}
});
...
}
private void processButtonA() {
// something here.
}
private void processButtonB() {
// something here.
}
then, if you need to call process related with the button B from button A, you can do the following:
buttonA.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
processButtonA();
processButtonB();
}
});

Different instructions for one button

I want to do different things for one button.
But I am changing instructions after the button was clicked every time.
First I am creating two buttons, after I clicked the button I want it to change its text. For each text I want to create different instruction.
I created an if/else for this. But I am not sure why my isn’t not working.
I only do instructions in the first statement.
Here is my code.
package com.example.iqbal.destini;
public class MAINPAGE extends AppCompatActivity {
Button mAnswer_1_Button;
Button mAnswer_2_Button;
TextView mTextbody;
int counter1 = 0;
int counter2 = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainpage);
mAnswer_1_Button = (Button) findViewById(R.id.answer_1);
mAnswer_2_Button = (Button) findViewById(R.id.answer_2);
mTextbody = (TextView) findViewById(R.id.text_body);
if(mTextbody.getText().toString().equals(getResources().getString(R.string.T1_Story)) && mAnswer_2_Button.getText().toString().equals(getResources().getString(R.string.T1_Ans2))&&mAnswer_1_Button.getText().toString().equals(getResources().getString(R.string.T1_Ans1)) ){
mAnswer_1_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextbody.setText(R.string.T3_Story);
mAnswer_1_Button.setText(R.string.T3_Ans1);
mAnswer_2_Button.setText(R.string.T3_Ans2);
}
});
mAnswer_2_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextbody.setText(R.string.T2_Story);
mAnswer_1_Button.setText(R.string.T2_Ans1);
mAnswer_2_Button.setText(R.string.T2_Ans2);
}
});
}
else if(mTextbody.getText().toString().equals(getResources().getString(R.string.T2_Story))&& mAnswer_2_Button.getText().toString().equals(getResources().getString(R.string.T2_Ans2))&&mAnswer_1_Button.getText().toString().equals(getResources().getString(R.string.T2_Ans1))){
mAnswer_1_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextbody.setText(R.string.T3_Story);
mAnswer_1_Button.setText(R.string.T3_Ans1);
mAnswer_2_Button.setText(R.string.T3_Ans2);
}
});
mAnswer_2_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextbody.setText(R.string.T4_End);
mAnswer_1_Button.setVisibility(View.GONE);
mAnswer_2_Button.setVisibility(View.GONE);
}
});
}
else if(mTextbody.getText().toString().equals(getResources().getString(R.string.T3_Story))&& mAnswer_2_Button.getText().toString().equals(getResources().getString(R.string.T3_Ans2))&&mAnswer_1_Button.getText().toString().equals(getResources().getString(R.string.T3_Ans1))){
mAnswer_1_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextbody.setText(R.string.T6_End);
mAnswer_1_Button.setVisibility(View.GONE);
mAnswer_2_Button.setVisibility(View.GONE);
}
});
**mAnswer_2_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextbody.setText(R.string.T5_End);
mAnswer_1_Button.setVisibility(View.GONE);
mAnswer_2_Button.setVisibility(View.GONE);
}
});
}
}
}**
mTextbody.getText() always is empty where it's currently located (unless you have a default string in the TextView)
Move the if conditions into the onClick method body.
Conditionally perform the action. Don't conditionally add the listener.
And you only need one OnClickListener per button

How to add Multi OnClickListener on button in Android

I want to add multi onclickListener On a button. Like if I click on Button1 then It will run Log.i("first","first") and then Log.i("Second","Second") but both Log.i not in one function/class.
You can just call it at the same time in onClick of your button:
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
run1();
run2();
}
});
and your functions:
public void run1(){
Log.i("first", "first");
}
public void run2(){
Log.i("Second", "Second");
}

Android Application crash on dialog Button click

Im trying to make a Dialog close when button is pressed.But Every time i press the button the application crashes.
public class CanvasPaint extends Activity implements OnClickListener {
final Button widthbtn = (Button) findViewById(R.id.widthbtn);
final Button widthpopBtn = (Button) findViewById(R.id.widthpopBtn);
final Context context = this;
final Dialog widthDialog = new Dialog(context);
widthbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
widthpopBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
widthDialog.dismiss();
}
});
widthDialog.show();
}
});
}
That's some inception code. Why are you setting a click listener inside a click listener? It should be more like
widthbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
widthDialog.show();
}
});
widthpopBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
widthDialog.dismiss();
}
});
Your dialog layout is not properly inflated, and therefore, your widthpopBtn will be null. Try inflating the layout like this:
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.your_dialog_layout, null);
Button widthpopBtn = (Button) dialogView.findViewById(R.id.widthpopBtn);

Variable visibility in setOnClickListener

I want to disable button B when making a treatement in button A.
public class GoJump extends Activity{
Button answerA, answerB;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jump);
answerA = (Button) findViewById(R.id.button_A);
answerB = (Button) findViewById(R.id.button_B);
answerA.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
// Do something
// Disable button B
}
});
The problem is button B is not visible inside button A treatement. I have to declare it another time to disable it.
Is there any other method to do? Make a variable visible in all class.
Thank you.
Initialize your variable before onCreate().
// Before onCreate
private Button b, b2;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b = (Button) findViewById(R.id.button);
b2 = (Button) findViewById(R.id.button2);
b.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
b2.setEnabled(false);
}
});
{
You just need to setEnabled to false. Read this javadoc.
public void onClick(View v)
{
answerB.setEnabled(false);
}

Categories