As my below codes shows that once button five and six shows with the titlesfourq1.
I click buttonfive it will show numten then if I click numten it will show numsix. But how do I show numsix with numten in the same layout?
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<AbsoluteLayout android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:background="#drawable/buttonsix" android:id="#+id/radio0"
android:layout_width="40dip" android:layout_height="40dip" android:layout_marginTop="50dip" android:layout_x="20dip" android:layout_y="55dip">
</Button>
<Button android:background="#drawable/buttonfive" android:id="#+id/radio1"
android:layout_width="40dip" android:layout_height="40dip" android:layout_marginTop="150dip" android:layout_x="200dip" android:layout_y="200dip">
</Button>
</AbsoluteLayout>
<ViewFlipper android:id="#+id/ViewFlipper01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_x="20dip">
<!--adding views to ViewFlipper-->
<TextView
android:id="#+id/title"
android:textStyle="bold"
android:textColor="#ffffff"
android:gravity="center_vertical|center_horizontal"
android:text="#string/titlesfourq1" android:layout_height="wrap_content" android:layout_y="10dip" android:layout_x="5dip" android:textSize="20dip" android:layout_width="340dp"/>
<Button android:text="#string/numten" android:id="#+id/number" android:textStyle="bold" android:background="#color/translucent_black" android:textColor="#ffffff"
android:layout_width="40dip" android:layout_height="40dip" android:layout_marginTop="150dip" android:layout_x="200dip" android:layout_y="200dip">
</Button>
<Button android:text="#string/numsix" android:id="#+id/number" android:textStyle="bold" android:background="#color/translucent_black" android:textColor="#ffffff"
android:layout_width="40dip" android:layout_height="40dip" android:layout_marginTop="150dip" android:layout_x="200dip" android:layout_y="200dip">
</Button>
<TextView android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second view is now displayed"></TextView>
<TextView android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Third view is now displayed"></TextView>
</ViewFlipper>
</AbsoluteLayout>
Java:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ViewFlipper;
public class Sfourq1 extends Activity {
Button RB0;
Button RB1;
Button RB2;
ViewFlipper VF;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sfourq1);
/*
* Find the views declared in main.xml.
*/
RB0 = (Button) findViewById(R.id.radio0);
RB1 = (Button) findViewById(R.id.radio1);
RB2 = (Button) findViewById(R.id.number);
VF = (ViewFlipper) findViewById(R.id.ViewFlipper01);
/*
* Set a listener that will listen for clicks on the radio buttons and
* perform suitable actions.
*/
RB0.setOnClickListener(button_one);
RB1.setOnClickListener(button_two);
RB2.setOnClickListener(button_test);
}
/*
* Define a OnClickListener that will change which view that is displayed by
* the ViewFlipper
*/
private OnClickListener button_one = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.radio0:
VF.setDisplayedChild(0);
Button button = (Button) v;
button.setVisibility(View.INVISIBLE);
break;
}
}
};
private OnClickListener button_two = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.radio1:
VF.setDisplayedChild(1);
Button button = (Button) v;
button.setVisibility(View.INVISIBLE);
break;
}
}
};
private OnClickListener button_test = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.number:
VF.setDisplayedChild(2);
Button button = (Button) v;
button.setVisibility(View.INVISIBLE);
Intent StartGameIntent = new Intent(Sfourq1.this,Home.class);
startActivity(StartGameIntent);
break;
}
}
};
}
for this you can give same tag to both buttons in different layout and whenever flip moves(can get by touch event)change focus to button with same tag.
thanx
Related
I'm building a Contact app using ListView. My ListView has 2 button. In this app, to test the respond ability of the buttons, I intended to set the button "Edit" so that when I click on it, it will change to "Clicked", and the button "Delete" will change to "Clicked", too. When I ran the Debug, the app stopped working and I couldn't get it work again (before I added the onClickListener, this app had worked property).
I don't know what is the error, and have tried many ways to fix.
Here is my row_listview.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="5">
<ImageView
android:id="#+id/imgAvatar"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:src="#drawable/if_male3_403019" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2.7"
android:orientation="vertical"
android:weightSum="2">
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:paddingLeft="15dp"
android:text="Akai Shuichi"
android:textSize="16dp"
android:textColor="#000"
android:gravity="center_vertical"
/>
<TextView
android:id="#+id/tvNumber"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:paddingLeft="15dp"
android:text="0982xxxxxx"
android:textSize="16dp"
android:textColor="#000"
android:gravity="center_vertical"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.3"
android:weightSum="2"
android:orientation="vertical">
<Button
android:id="#+id/btnEdit"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:backgroundTint="#3FE0FF"
android:onClick="myClickHandler"
android:text="Edit"
android:textColor="#fff"
android:textStyle="bold" />
<Button
android:id="#+id/btnDelete"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:backgroundTint="#F73131"
android:onClick="myClickHandler"
android:text="Delete"
android:textColor="#fff"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
And here is my MainActivity.java:
package com.huy9515gmail.mycontact;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
#BindView(R.id.edt_inputName) EditText edtName;
#BindView(R.id.btnAdd) Button btnAdd;
#BindView(R.id.btnEdit) Button btnEdit;
#BindView(R.id.btnDelete) Button btnDelete;
#BindView(R.id.edt_inputNumber) EditText edtNumber;
#BindView(R.id.rdbtn_male) RadioButton rdbtn_male;
#BindView(R.id.rdbtn_female) RadioButton rdbtn_female;
#BindView(R.id.rdbtn_others) RadioButton rdbtn_others;
#BindView(R.id.gender) RadioGroup genderSelection;
private ListView lvContact;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
lvContact = (ListView) findViewById(R.id.lv_contact);
final ArrayList<Contact> arrContact = new ArrayList<>();
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//validating contact info
if (((edtName.getText().toString().trim()) == "") || (edtNumber.getText().toString().trim() == "") || ((rdbtn_male.isChecked()==false) && (rdbtn_female.isChecked()==false) &&(rdbtn_others.isChecked()==false))) {
Toast.makeText(MainActivity.this, "Invalid contact info! Please try again!", Toast.LENGTH_SHORT).show();
}
//adding contact info
else {
Contact contact = new Contact(Gender.male, "", "");
//adding info
contact.setName(edtName.getText().toString());
contact.setNumber(edtNumber.getText().toString());
arrContact.add(contact);
}
CustomAdapter customAdapter = new CustomAdapter(MainActivity.this, R.layout.row_listview, arrContact);
lvContact.setAdapter(customAdapter);
}
});
}
public void myClickHandler(View v) {
LinearLayout vwParentRow = (LinearLayout) v.getParent();
Button btnEdit = (Button) vwParentRow.getChildAt(0);
Button btnDelete = (Button) vwParentRow.getChildAt(1);
btnEdit.setText("Clicked");
btnDelete.setText("Clicked");
}
}
And here is the row_listview.xml layout:
Instead of setting the android:onClick in XML, do the same as you did for btnAdd.
Find the view by id or use butterknife, then put the following in onCreate()
btnEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Your code here
}
});
btnDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Your code here
}
});
Make sure you have an element in your XML file with the id like this:
android:id="#+id/btnAdd"
I've asked a question similar to this. The point of this code is to check if a checklist is checked, if it is than it adds 1 to the score.
I've been told that the problem is that I haven't set an event. Please explain what I need to do to make it work, I'm new to android development and have been stuck on this for a few days. Please give a code example with your answer.
package xyz.ashraf.whoisdelasalle;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
/**
* Created by Ashraf on 3/2/2016.
*/
public class check_Button extends Pop_sallian{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popwindow_sallian);
// Connects The variable to an xml id
TextView output = (TextView) findViewById(R.id.output);
final int[] score = {0};
//sets the variable to 0
OnCheckedChangeListener checkedListener = new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.concern:
score[0]++;
break;
case R.id.faith:
score[0]++;
break;
case R.id.respect:
score[0]++;
break;
case R.id.education:
score[0]++;
break;
case R.id.community:
score[0]++;
break;
}
}
};
// adds the variables together to form a score
if(score[0] == 0){
output.setText("Come on! Get involved, your la sallian community needs you.");
} else if(score[0] == 1){
output.setText("Good start, keep going!");
} else if(score[0] == 2){
output.setText("Room to improve but doing good!");
} else if(score[0] == 3){
output.setText("Very good, others look up to you!");
} else if(score[0] == 4){
output.setText("Wow, you really are an inspiration");
} else if(score[0] == 5){
output.setText("Excellent! You're a leader in your la sallian community. Nice work!");
} else{
output.setText("Unknown");
}
// changes the output text based on score value
}
}
^^checklist code^^
package xyz.ashraf.whoisdelasalle;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.who);
Button today = (Button) findViewById(R.id.today);
Button sallian = (Button) findViewById(R.id.sallian);
Button how = (Button) findViewById(R.id.toBe);
Button moreInfo = (Button) findViewById(R.id.info);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Pop.class));
}
});
today.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Pop_today.class));
}
});
sallian.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Pop_sallian.class));
}
});
how.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Pop_how.class));
}
});
moreInfo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Pop_info.class));
}
});
}
}
^^Code where the button is and, when pressed calls on the checklist code, The Ok button is to close the pop up when pressed^^
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="match_parent" android:layout_height="match_parent">
android:elevation="8dp"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Are you a Sallian?"
android:id="#+id/textView7"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30sp"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you meet the following prerequisites, if you do you may be a Sallian"
android:id="#+id/textView8"
android:layout_below="#+id/textView7"
android:layout_centerHorizontal="true"
android:textSize="20sp"
android:textColor="#000000" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Are you concerened for the poor and Social Justice?"
android:id="#+id/concern"
android:textSize="18sp"
android:layout_below="#+id/textView8"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you have faith in the presence of God?"
android:id="#+id/faith"
android:textSize="15sp"
android:layout_below="#+id/concern"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you have Respect for all people?"
android:id="#+id/respect"
android:layout_below="#+id/faith"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp"
android:textSize="15sp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you value education?"
android:id="#+id/education"
android:textSize="15sp"
android:layout_below="#+id/respect"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Are you inclusive in your community?"
android:id="#+id/community"
android:layout_below="#+id/education"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="15sp"
android:checked="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok"
android:id="#+id/okButton_sallian"
android:layout_below="#+id/community"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="20dp"
android:layout_marginTop="90dp"
android:layout_marginBottom="20dp"
android:background="#FAFAFA"
android:textColor="#00E676"
android:elevation="2dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check"
android:id="#+id/check"
android:textColor="#00E676"
android:elevation="2dp"
android:background="#FAFAFA"
android:layout_alignTop="#+id/okButton_sallian"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/output"
android:textColor="#1eff00"
android:textSize="23sp"
android:layout_below="#+id/community"
android:layout_centerHorizontal="true"
android:layout_above="#+id/check" />
</RelativeLayout>
</ScrollView>
^^ xml code the code takes for^^
You are defining a checked listener, but doing nothing with it. You need to set it to your checkbox or checkboxes.
mCheckBox = (CheckBox) findViewById(R.id.concern);
mCheckBox.setOnCheckedChangedListener(checkedListener);
Just like you do on your buttons.
i displayed a expression on the screen "2+3=?" and what i want to do is when i press 5 the question mark gets replaced by 5 and when i press # displays "correct" in green
package org.example.question;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class QuestionActivity extends Activity implements View.OnClickListener {
/** Called when the activity is first created. */
//variable for different questions
//variable and type declaration for buttons and text
TextView display;
TextView displayGuess;
TextView displayQuestion;
TextView Correct;
TextView guess;
TextView question;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//display text on screen
displayGuess = (TextView) findViewById(R.id.Guess);
displayQuestion = (TextView) findViewById(R.id.Question);
//Creating and setting onClickerLIstener on each button
Button one = (Button) findViewById(R.id.keypad_1);
one.setOnClickListener(this);
Button two = (Button) findViewById(R.id.keypad_2);
two.setOnClickListener(this);
Button three = (Button) findViewById(R.id.keypad_3);
three.setOnClickListener(this);
Button four = (Button) findViewById(R.id.keypad_4);
four.setOnClickListener(this);
Button five = (Button) findViewById(R.id.keypad_5);
five.setOnClickListener(this);
Button six = (Button) findViewById(R.id.keypad_6);
six.setOnClickListener(this);
Button seven = (Button) findViewById(R.id.keypad_7);
seven.setOnClickListener(this);
Button eight = (Button) findViewById(R.id.keypad_8);
eight.setOnClickListener(this);
Button nine = (Button) findViewById(R.id.keypad_9);
nine.setOnClickListener(this);
Button del = (Button) findViewById(R.id.keypad_delete);
del.setOnClickListener(this);
Button zero = (Button) findViewById(R.id.keypad_0);
zero.setOnClickListener(this);
Button hash = (Button) findViewById(R.id.keypad_hash);
hash.setOnClickListener(this);
Button minus = (Button) findViewById(R.id.keypad_minus);
minus.setOnClickListener(this);
}
//method to tell what each button does
public void onClick(View argh0) {
switch(argh0.getId()) {
case R.id.keypad_hash:
if("2+3=5".equals(display.getText().toString()))
{
display.setText("CORRECT".setColor."#00ff00");
}
break;
case R.id.keypad_5:
String str5 = display.getText().toString();
display.setText(str5.replace("?","5"));
break;
case R.id.keypad_1:
display.setText("1");
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/Guess"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="Guess: "
android:textSize="25dp" />
<TextView
android:id="#+id/Question"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="2+3=?"
android:textSize="25dp" />
<TextView
android:id="#+id/CORRECT"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text=""
android:textSize="25dp" />
<TextView
android:id="#+id/INCORRECT"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text=""
android:textSize="25dp" />
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/keypad"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*" >
<TableRow>
<Button android:id="#+id/keypad_1"
android:text="1" >
</Button>
<Button android:id="#+id/keypad_2"
android:text="2" >
</Button>
<Button android:id="#+id/keypad_3"
android:text="3" >
</Button>
</TableRow>
<TableRow>
<Button android:id="#+id/keypad_4"
android:text="4" >
</Button>
<Button android:id="#+id/keypad_5"
android:text="5" >
</Button>
so far i can display the question but when i press "5" the application crashes
really will appreciate the help
I don't know whether you posted the whole code or not, but from what I see you never initialize the variable "display" (TextView display;). Then when you use it you will get a NullPointerException.
I think you missed a line like display = (TextView) findViewById(R.id.display..);
I have created following Custom Preference Screen.
I want to add Listener on Button1 and Button2. How should I do it?
I am using following code for creating above Preference Screen.
DemoPref:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class DemoPref extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startActivity(new Intent(this, MyPref.class));
}
}
MyPref:
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MyPref extends PreferenceActivity{
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//setContentView(R.layout.tftp_setting);
this.addPreferencesFromResource(R.xml.list_pref);
/*Button b1 = (Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Log.i("MyPref", "Button1 one is clicked.");
}
});
Button b2 = (Button)findViewById(R.id.button2);
b2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Log.i("MyPref", "Button2 one is clicked.");
}
});*/
}
}
res\layout\setting.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal">
<TextView android:text="TextView1" android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:text="EditText2" android:id="#+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal">
<TextView android:text="TextView2" android:id="#+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:text="EditText2" android:id="#+id/editText2" android:layout_height="wrap_content" android:layout_width="wrap_content"></EditText>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:text="Button1" android:id="#+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Button2" android:id="#+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
res\xml\pref.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceScreen android:title="My Setting" android:layout="#layout/setting" android:summary="This is my setting."></PreferenceScreen>
</PreferenceScreen>
You need:
Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("MyPref", "Button1 one is clicked.");
}
});
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("MyPref", "Button2 one is clicked.");
}
});
I don't have an answer but have some insight -
The view in which you are trying to find the buttons is NOT settings.xml (or pref.xml) - that's why findViewById returns null.
I think the view (layout) which is loaded is internal Android OS view, with ID 17367111 (or 0x1090047).
The only preference you have in the PreferenceScreen you open is another PreferenceScreen (which uses the settings.xml as layout)
you need to get access to the view of this internal PreferenceScreen and them you'd be able to do:
prefScreeView.findViewByID(R.id.button1);
Hope it helps someone to find the answer.
Button btn = (Button) getPreferenceScreen().findPreference(key);
Developing my first app for android. Works OK on emulator, but on phone I get a "Source not found error" ViewRoot.handleMessage(Message)line:1757. I get the error when I press button number 4 on the application and try to display my media images on phone.
Code:
package com.example.famiily_connect_v1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class family_connect extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button incrementButton, decrementButton, makecallButton,
media_cameraButton;
TextView numberdisplayTextView;
EditText inputEditText;
int A = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
incrementButton = (Button) findViewById(R.id.Button01);
decrementButton = (Button) findViewById(R.id.Button02);
makecallButton = (Button) findViewById(R.id.Button03);
media_cameraButton = (Button) findViewById(R.id.Button04);
numberdisplayTextView = (TextView) findViewById(R.id.TextView01);
inputEditText = (EditText) findViewById(R.id.EditText01);
incrementButton.setOnClickListener(this);
decrementButton.setOnClickListener(this);
makecallButton.setOnClickListener(this);
media_cameraButton.setOnClickListener(this);
numberdisplayTextView.setOnClickListener(this);
inputEditText.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.Button01:
A++;
numberdisplayTextView.setText(String.valueOf(A));
break;
case R.id.Button02:
A--;
numberdisplayTextView.setText(String.valueOf(A));
break;
case R.id.Button03:
break;
case R.id.Button04:
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivity(myIntent);
break;
default:
break;
}
}
}
XML
<?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" android:background="#00AAAA">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="#string/hello" />
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="wrap_content" android:layout_height="wrap_content"></LinearLayout>
<Button android:id="#+id/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="increment"></Button>
<Button android:id="#+id/Button02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="decrement"></Button>
<Button android:id="#+id/Button03" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="make call"></Button>
<Button android:id="#+id/Button04" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="media_camera"></Button>
<TextView android:id="#+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="24sp"
android:text="0" android:textStyle="bold" android:textColor="#000000"></TextView>
<EditText android:id="#+id/EditText01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="enter new button text"></EditText>
<RadioButton android:id="#+id/RadioButton01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="camera"></RadioButton>
</LinearLayout>
Enable in your Eclipse the LogCat
Window > Show View > Other > Android >
LogCat
then you can find what is the real description of your Exception.