I have been able to pass data to other activities except this one. Can anyone see what I'm doing wrong. The only error i'm getting is that my TextView showmsg is NOT showing up in the new activity. Does anyone know why?
public class MyScanActivity extends Activity
{
private static final String MY_CARDIO_APP_TOKEN = "NOT THE PROBLEM";
final String TAG = getClass().getName();
private Button scanButton;
private TextView resultTextView;
private Button buttonBack;
private TextView showmsg;
private int MY_SCAN_REQUEST_CODE = 100; // arbitrary int
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.myscan);
Intent in = getIntent();
if (in.getCharSequenceExtra("usr") != null) {
final TextView setmsg = (TextView)findViewById(R.id.showmsg);
setmsg.setText(in.getCharSequenceExtra("usr"));
}
resultTextView = (TextView)findViewById(R.id.resultTextView);
scanButton = (Button)findViewById(R.id.scanButton);
buttonBack = (Button)findViewById(R.id.buttonBack);
showmsg = (TextView) findViewById(R.id.showmsg);
There are not too many options for how your text can be not shown.
You have the view itself messed up: Check this by putting some sample text in the XML file using android:text="TEST" in the TextView showmsg. Your text should appear, unless your text is the wrong color or size, or something else happens to be above it.
You aren't actually finding it with findViewById() (I hope you've double checked that in a debugger) I agree with alex that you might not want R.id.showmsg. Did you mean to put R.id.resultTextView there instead?
Your passed text is not actually coming through. You should do a log statement, like Log.v(TAG, "Passed text is " + in.getCharSequenceExtra("urs")); and make sure the text is actually coming through.
I haven't test it. but i think that is the reason.
change here:
if (in.getCharSequenceExtra("usr") != null) {
final TextView setmsg = (TextView)findViewById(R.id.showmsg);
setmsg.setText(in.getCharSequenceExtra("usr"));
}
with this:
showmsg = (TextView) findViewById(R.id.showmsg);
if (in.getCharSequenceExtra("usr") != null) {
showmsg.setText(in.getCharSequenceExtra("usr"));
}
Related
I passed the data from the first actvity (like int VNIMANI and int OBRATNOST). This data set my TextView after I came on second activity. now I need a button. And when I click on the button I need increment +1. But the code doesn't work.
TextView tvPaklic = (TextView) findViewById(R.id.paklic);
String paklic = String.valueOf( + 10+ (VNIMANI+OBRATNOST));
tvPaklic.setText(paklic);
}
int newPaklic = 0;
public void plusPaklic (View v){
newPaklic = newPaklic + 1;
displayPaklicDve (newPaklic);
}
private void displayPaklicDve(int newPaklic) {
TextView tvpaklicdve = (TextView) findViewById(R.id.paklic);
String paklicdve = String.valueOf(newPaklic);
tvpaklicdve.setText(paklicdve);
}
I would really like to suggest you refer this one Read this, will solve your problem
You were used 2 Textview objects with referring one id check that.
TextView tvPaklic = (TextView) findViewById(R.id.paklic);
TextView tvpaklicdve = (TextView) findViewById(R.id.paklic);
I have this piece of code:
public void ON_function(View view) {
int position = lv.getPositionForView(view);
String pozycja = Integer.toString(position);
String tile_content = listaurzadzen.get(position).toString().replace("{urzadzenie=", "\0").replace("}", "\0");
String[] IP_substring = tile_content.split("\r\n");
SendMessage(IP_substring[1].trim(), "switchon");
//ImageButton bulb = (ImageButton)findViewById(R.id.imageButton);
//bulb.setImageResource(R.drawable.bulb_on);
}
public void OFF_function(View view) {
int position = lv.getPositionForView(view);
String pozycja = Integer.toString(position);
String tile_content = listaurzadzen.get(position).toString().replace("{urzadzenie=", "\0").replace("}", "\0");
String[] IP_substring = tile_content.split("\r\n");
SendMessage(IP_substring[1].trim(), "switchoff");
//ImageButton bulb = (ImageButton)findViewById(R.id.imageButton);
//bulb.setImageResource(R.drawable.bulb_off);
}
Two simple functions which are called from dynamically created listview rows, each sends a message or whatever.
My problem is, if I can get the position using (view) to get the string of specific row, why can't I use it to change the image button resource of this specific row? Do I really have to create all these getView methods to make it happen? If so how do I do it with a simple adapter. I have been looking for a solution for a long time.
Can you do
ImageButton bulb = (ImageButton) view.findViewById(R.id.imageButton);
bulb.setImageResource(R.drawable.bulb_off);
I have MainText1 displays a text, when you click on button 3 in the Menu MainText1 displays text3 which is coming from the super class Text. What I want is that dynamically when you click on any button it reads the number and displayed the respective text, that's all about. ;)
I want to get rid of switch case in my activity, so I'm trying now for 2days :( to change the name of the string variable dynamically, but I think I'm using a wrong code as string variable is different from resources (confused), here's my code, this really challenging me this weekend:
public class MainText1 extends Text {
String
tx1=text1,tx2=text2,tx3=text3,
tx,stringReceived;//text1,text2...textn strings coming from the Super class Text
num = Integer.parseInt(getIntent().getStringExtra("somekey1")); // this data is coming from the menu, it depends on which button is clicked
tx="text"+num; // text is the name of the string variable, it should be in format like that : text1,text2,...textn which have predefined string content
stringId = getApplicationContext().getResources().getIdentifier(tx, "string", getPackageName());
if (stringId > 0) {
stringReceived=getApplicationContext().getResources().getString(stringId);
I guess what you're trying to do is to change a TextView contents. So, you could do the following:
public YourActivity extends Activity {
//Here you will declare your layout items
private Button button1;
private Button button2;
private Button button3;
private TextView txtView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
//Here you will get you layout elements
button1 = (Button) findViewById(R.id.button1_id);
button2 = (Button) findViewById(R.id.button2_id);
button3 = (Button) findViewById(R.id.button3_id);
txtView = (TextView) findViewById(R.id.txtview_id);
//Now you will have to set the onClickListeners
button1.addOnCLickListener(new OnClickListener() {
#Override
private onClick() {
//Set the text in the text view to the string related
//to button1
txtView.setText(getResources()
.getString(R.string.button1_string);)
}
});
button2.addOnCLickListener(new OnClickListener() {
#Override
private onClick() {
//Set the text in the text view to the string related
//to button2
txtView.setText(getResources()
.getString(R.string.button2_string);)
}
});
button3.addOnCLickListener(new OnClickListener() {
#Override
private onClick() {
//Set the text in the text view to the string related
//to button3
txtView.setText(getResources()
.getString(R.string.button3_string);)
}
});
}
}
This should do the trick. Although, like the other guys suggested it, you should take a look at some tutorials before coding on
I have a couple buttons that are hard coded that will need to store some specific data and provide that attribute when clicked such as, one button is "non-fiction" and the other "fiction". I need to use an attribute and not the button text since the button text may change down the line but the attribute is need for database calls.
That is, "non-fiction" could become "true stories" but "non-fiction" will still need to be returned.
I've done something similar programmatically with btn.setTag(...) and btn.getTag(...) but those buttons are generated based on the database not hard coded into the app.
How do I set a custom attribute to a button then retrieve it?
something like:
<Button
android:id="#+id/fictionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_weight="1"
android:onClick="showTools"
android:text="#string/fiction_button"
custom:bookType="fiction" />
<Button
android:id="#+id/nonfictionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_weight="1"
android:onClick="showTools"
android:text="#string/non_fiction_button"
custom:bookType="nonfiction" />
----- edit -----
I've changed my approach based on the answer so far.
I've set before the onCreate:
Button fictionButton;
Button nonfictionButton;
Inside the onCreate I've placed:
fictionButton = (Button) findViewById(R.id.fictionButton);
nonfictionButton = (Button) findViewById(R.id.nonfictionButton);
fictionButton.setTag("bookType","Fiction");
nonfictionButton.setTag("bookType", "Non Fiction");
when the button is clicked I'm getting the tag and storing to a SharedPreference
However now I'm getting an error at fictionButton.setType("bookType","Fiction"); ADT doesn't like the key and wants to remove it.
----- edit -----
The set tag is working but now the getTag is throwing the NullPointerException. I'm using Button b to target all buttons and attempting to get the tag when any button is clicked inside the onClick event. buttonID is initialized before the onCreate and declared as R.id.fictionButton in the onCreate:
b = (Button) view;
String buttonText = b.getTag(buttonID).toString();
----- edit -----
my java file before and with the onCreate:
public class Crossroads extends baseActivity {
FlyOutContainer root;
Button b;
Button fictionButton;
Button nonfictionButton;
Integer buttonID;
public static final String PREFS_NAME = "myPrefs";
SharedPreferences storedInfo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.root = (FlyOutContainer) this.getLayoutInflater().inflate(
R.layout.activity_crossroads, null);
setContentView(R.layout.activity_crossroads);
buttonID = R.id.fictionButton;
fictionButton = (Button) findViewById(R.id.fictionButton);
nonfictionButton = (Button) findViewById(R.id.nonfictionButton);
fictionButton.setTag(buttonID,"Fiction");
nonfictionButton.setTag(buttonID,"Non Ficiton");
this.setContentView(root);
}
...
}
----- edit -----
Fixed the final piece by comparing ids and setting the string accordingly:
Integer viewId = view.getId();
String buttonText;
// setContentView(R.layout.activity_crossroads);
if(viewId == R.id.fictionButton )
buttonText = fictionButton.getTag(buttonID).toString();
else
buttonText = nonfictionButton.getTag(buttonID).toString();
i think your main problem is that you app won't compile
because you have written:
setTag(String, String);
but the setTag(..) function doesn't take an String as key but an int
so you could do like this:
public static int KEY_BOOK_TYPE = 0; //some int
fictionButton = (Button)findViewById(R.id.fictionButton);
nonfictionButton = (Button)findViewById(R.id.nonfictionButton);
fictionButton.setTag(KEY_BOOK_TYPE, "Fiction");
nonfictionButton.setTag(KEY_BOOK_TYPE, "Non Fiction");
setType(String, String) doesn't exist in Button and so ADT wants to remove that line
--- edit ---
be careful and call:
setContentView(R.layout.your_layout);
before calling any findViewById(R.id.something) else it returns null
--- edit ---
if you call setTag(...) with the button id too, thats really good idea!
but you cannot simply cast any view to the button you want
you have to save the button you got from findViewById like this:
class MainActivity extends Activity
{
int non_fiction_id;
int fiction_id;
Button button_non_fiction;
Button button_fiction;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
non_fiction_id = R.id.button_non_fiction;
fiction_id = R.id.button_fiction;
button_non_fiction = (Button)findViewById(non_fiction_id);
button_fiction = (Button)findViewById(fiction_id);
}
void SomeOtherPlace()
{
String non_ficiton_tag = button_non_fiction.getTag(non_fiction_id);
}
}
In the code below I get runtime error in eclipse. Why this error is not displayed at compile time?
public class AndroidUIActivity extends Activity implements OnClickListener {
private static final int PROGRESS = 0x1;
private ProgressBar mProgress;
private int mProgressStatus = 0;
private int maxtime=0;
private Handler mHandler = new Handler();
int fileSize=0;
private MediaPlayer mp3player;
private TextView txt_Currenttime;
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
txt_Currenttime.setText(12); /* line with error */
}
}
First identify the Textview id
Text view txt_Currenttime = (TextView)findViewById(R.id.textviewid);
then set the value
txt_Currenttime.setText(String.valueOf(12));
You should have something like:
txt_Currenttime = (TextView)findViewById(R.id.textviewid);
txt_Currenttime.setText(String.valueOf(12));
before setting the text.
The Run time error is coming because you are setting integer value as a text of textview but textview is not take integer value, Change below line of your code
txt_Currenttime.setText(12);
to
txt_Currenttime.setText(String.valueOf(12)); or txt_Currenttime.setText("12");
And add below line after setContentView(R.layout.main);
txt_Currenttime = (TextView)findViewById(R.id.mTxtView1);
it will solve your problem.
You should have to first find the id of that textview after that you can apply any operation on that textview. you are not initializing the textview but you are using it so first use the below code then set text. and you have to set the text in double quotes.
txt_Currenttime = (TextView)findViewById(R.id.textviewid);
txt_Currenttime.setText("12");
txt_Currenttime.setText(12);
In this line ?
The textView needs to be set using a String
txt_Currenttime.setText("12");
In java private TextView txt_Currenttime means that you just have a reference, so you need to build an object (with a new) before using txt_Currenttime.