When I click a button, I want the the string #string/dummy_content in fullscreen_content to change. How can I do that?
xml:
<TextView android:id="#+id/fullscreen_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:textColor="#2f4b66"
android:textStyle="bold"
android:textSize="50sp"
android:gravity="center"
android:text="#string/dummy_content">
button xml:
<Button android:id="#+id/dummy_button"
style="?metaButtonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/dummy_button"
android:clickable="true"
android:onClick="printStarter"
/>
The Java function I would like to activate:
public void printStarter(View view) {
}
Try this..
You can't change string resource at the run time
public void printStarter(View view) {
((TextView)findViewById(R.id.fullscreen_content)).setText("Your Text");
}
You need to get the reference of the TextView whose text you need to set. Here is the code:
public void printStarter(View view){
TextView text=(TextView)findViewById(R.id.fullscreen_content);
text.setText("Some text....");
}
You can't change string resource dynamically since it's a compiled resource.
Related
Resolved: The issue was with the java not running, not with the code itself. Fixed that and the button worked.
My button does (seemingly) nothing when I push it in the emulator. I'm attempting to have text update when a button is pushed. I started off by reading the Android Dev Button Guide, and other answered questions, but even when directly copy-pasting the code voted as the correct answer from numerous questions, and updating with the names of my items, I still can't get it working.
This is the current java code:
public class PlusMinus extends Activity {
TextView mcvalue;
Button sub;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
mcvalue= (TextView) findViewById(R.id.mcvalue);
sub= (Button) findViewById(R.id.bSub5MC);
sub.setOnClickListener(new View.OnClickListener() {
public void onClick(View paramView) {
mcvalue.setText("Click");
}
});
}}
This is the corresponding .xml for the text I want to update, and the button. There is more .xml surround it relating to the table layout I used, but I don't think that is relevant (if it is I can add the whole thing in).
<Button
android:id="#+id/bSub5MC"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:keepScreenOn="false"
android:text="#string/subtract_five"
android:textSize="20sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#FFD700"
android:layout_gravity="center_vertical|center_horizontal"
android:id="#+id/mcvalue"
android:text="#string/megacredits_name"
android:textColor="#000000"
android:textSize="20sp" />
I want to replace the text of #string/megacredits_name displayed as "MC" with "Click." Eventually my plan is to do some adding/subtracting with it, but trying to figure out the simplest building block first before expanding on it.
Thank you
Edit: LogCat removed as it wasn't helpful.
change android:layout_width="0dp" to android:layout_width="wrap_content" of TextView and Button.
and then ID to the TextView i.e. android:id="#+id/mcvalue"
<TextView
android:id="#+id/mcvalue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFD700"
android:layout_gravity="center_vertical|center_horizontal"
android:text="#string/megacredits_name"
android:textColor="#000000"
android:textSize="20sp" />
in Layout file put layout_weight:
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="#+id/bSub5MC"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:keepScreenOn="false"
android:text="click"
android:textSize="20sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#FFD700"
android:id="#+id/mcvalue"
android:layout_weight="1"
android:layout_gravity="center_vertical|center_horizontal"
android:text="name"
android:textColor="#000000"
android:textSize="20sp" />
</LinearLayout>
and in java file:
TextView mcvalue;
Button sub;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.item_check);
mcvalue= (TextView) findViewById(R.id.mcvalue);
sub= (Button) findViewById(R.id.bSub5MC);
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mcvalue.setText("click");
}
});
}
my problem is that I want a Radio Group that has 3 Radio Buttons, using the scheme below.
The three choices are:
1. [] Male
2. [] Female
3. [] Custom: (self-described identity)
However, the problem is that I want the user to type in their self-described identity into an EditText for me to retrieve.
So the following code is from my XML page, with some elements blocked out by "####".
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="####"
android:id="#+id/male_female_custom_choice"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<RadioButton android:id="#+id/radio_button_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_button_male"
android:checked="true" />
<RadioButton android:id="#+id/radio_button_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_button_female"
android:checked="false" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="####"
android:weightSum="1">
<RadioButton
android:id="#+id/radio_button_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_button_custom"
android:checked="false" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="####"
android:hint="####"
android:focusableInTouchMode="true"
android:gravity="center"
android:layout_weight="1.05"
android:textSize="14sp" />
<TextView
android:layout_width="42dp"
android:layout_height="43dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="####"
android:id="####"
android:singleLine="true"
android:gravity="center"
android:layout_marginLeft="0dp"
android:textColor="#000000" />
</LinearLayout>
</RadioGroup>
As you can see, I have tried to use a LinearLayout to isolate the custom option.
However, there are unintended and undesired side effects.
1. The custom option can be selected in addition to the other 2 predefined genders.
2. The custom option cannot be selected on its own.
In the actual Java file for the activity, I have the following code:
// button, radio button, editText, and Spinner fields
public EditText mEdit;
public RadioButton rButton;
public RadioGroup rSexGroup;
rSexGroup = (RadioGroup)findViewById(R.id.male_female_custom_choice);
// get selected radio button from RadioGroup
int selectedId = rSexGroup.getCheckedRadioButtonId();
// find radio button by returned id
rButton = (RadioButton)findViewById(selectedId);
// assign gender based on id of radio button
if (selectedId == 1) {
pat.gender = "male";
}
if (selectedId == 2) {
pat.gender = "female";
}
if (selectedId == 3) {
mEdit = (EditText)findViewById(R.id.####);
pat.gender = (mEdit.getText().toString());
}
Since I am a bit rusty with Java, it may be possible that I have some really newbish errors. Please advise.
Once again, I am looking for a way to get a set of 3 RadioButtons, each on an individual line, with the last RadioButton with an EditText adjacent to it from which I obtain the desired information.
EDIT: Here's a picture of what I want it to look like:
(http://i68.tinypic.com/ao2oow.png)
Unfortunately I need 10 reputation to post images. :(
Mohit's answer gives the EditText on a different line than the custom input.
(http://i63.tinypic.com/68u88x.png)
Please note that the orientation of the EditText is adjacent to the custom, and not below. I apologize for not clearly specifying enough what I wanted.
Because selectedId will not be 1,2 or 3....debug it you will get value..
The custom option cannot be selected on its own.
Remove your 3rd RadioButton from LinearLayout and replace below 2nd RadioButton and put your EditText and TextView inside LinearLayout..
On you listener get getCheckedRadioButtonId and getText() of that RadioButton and check it accordingly...
I dont no what is your task but here is how can get all three RadioButton working and get custom text too....
xml...
UPDATE
<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"
tools:context="com.ex.MainActivity" >
<RadioGroup
android:id="#+id/male_female_custom_choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="#+id/radio_button_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Male" />
<RadioButton
android:id="#+id/radio_button_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="FeMale" />
<RadioButton
android:id="#+id/radio_button_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="Custom" />
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/male_female_custom_choice"
android:layout_toRightOf="#+id/male_female_custom_choice"
android:orientation="horizontal"
android:weightSum="1" >
<EditText
android:id="#+id/edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".8"
android:ems="10"
android:focusableInTouchMode="true"
android:gravity="center"
android:hint="aa"
android:inputType="text"
android:textSize="14sp" />
<TextView
android:id="#+id/text"
android:layout_width="42dp"
android:layout_height="43dp"
android:layout_marginLeft="0dp"
android:layout_weight=".2"
android:gravity="center"
android:singleLine="true"
android:text="aaa"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
</LinearLayout>
<Button
android:id="#+id/but"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/male_female_custom_choice"
android:text="Get" />
</RelativeLayout>
.java file..
public class MainActivity extends Activity {
public EditText mEdit;
public RadioButton rButton;
public RadioGroup rSexGroup;
public Button but;
public String str = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rSexGroup = (RadioGroup)findViewById(R.id.male_female_custom_choice);
but= (Button) findViewById(R.id.but);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int selectedId = rSexGroup.getCheckedRadioButtonId();
rButton = (RadioButton)findViewById(selectedId);
if (rButton.getText().toString().equals("Male")) {
str = "Male";
}
if (rButton.getText().toString().equals("FeMale")) {
str = "FeMale";
}
if (rButton.getText().toString().equals("Custom")) {
mEdit = (EditText)findViewById(R.id.edit);
str = mEdit.getText().toString();
}
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
});
}
}
you can also set visibility of LinearLayout so that it only visible when custom in checked....
Hope it help..
put radio group in RelativeLayout
set third radiobutton text as empty/null
add EditText as layout_alignParentBottom="true"
now programmatically call EditText's onFocusChangeListener
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b)
thirdRadio.setCheched(true);
}
});
I have two layouts. I want to keep one layout gone when the activity is loaded and it should be visible onClick of another layout. so I have added this code OnClickListener of the layout.
additionalContactFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(linearLayoutFrom.getVisibility() == View.GONE){
linearLayoutFrom.setVisibility(View.VISIBLE);
}else{
linearLayoutFrom.setVisibility(View.GONE);
}
}
});
And have set visibility of the layout gone in xml file..
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/LinearLayoutAdditionalContactFrom">
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/imageView13"
android:layout_marginLeft="20dp"
android:background="#drawable/ic_person_black_48dp"
android:layout_marginTop="05dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_marginRight="10dp"
android:drawableRight="#drawable/ic_expand_more_black_24dp"
android:text="Additional contact (optional)"
android:cursorVisible="false"
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginRight="50dp"
android:layout_gravity="center"
android:visibility="gone"
android:id="#+id/LinearLayoutFrom">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText2"
android:layout_weight="1"
android:hint="Name"
android:layout_gravity="center"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText3"
android:layout_weight="1"
android:hint="Phone"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="OR"
android:id="#+id/textView19"
android:layout_gravity="center" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/textView13"
android:layout_marginLeft="48dp"
android:hint="Input if you're not receiver" />
</LinearLayout>
Unable to understand whats going wrong.. The listener is not getting called at all.. Please help..
Your problem is that your EditText is capturing the click event when you click on it. If you click somewhere else in the LinearLayout it should work.
You can replace the EditText with a TextView if you don't need the user to edit the content.
Change
linearLayoutFrom.setVisibility(View.VISIBLE);
To
findViewById(R.id.YOURLAYOUTID).setVisibility(View.VISIBLE);
I think you can't access to local vars in sub methods
It Seems That Layout Visibility is Already set to GONE, so Onlick listener is not working on hidden View and it should not work too.
INVISBLE means you are trying to add a listener to a view which is not there. You can add the listener to a visible view only.
WORKAROUND
1) Try to make a dummy view which is visible but having the same color as background.
2) Try to set the listener for parent and check the position (whether the position does
belongs to INVISIBLE view).
Please try to set the onClickListener to the EditText and to the ImageView and no to the LinearLayout
The problem is that the Handler of the EditText is most important that the LinearLayout handler.
Almost you can try to make a break point to the OnClick and see what is happend
This is an example to explain you how to do that:
in MainActivity Class:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
private LinearLayout linearLayoutFrom;
private LinearLayout additionalContactFrom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
additionalContactFrom = (LinearLayout) findViewById(R.id.LinearLayoutAdditionalContactFrom);
linearLayoutFrom = (LinearLayout) findViewById(R.id.LinearLayoutFrom);
linearLayoutFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"linearLayoutFrom clicked!!!", Toast.LENGTH_SHORT)
.show();
if (additionalContactFrom.getVisibility() == View.GONE) {
additionalContactFrom.setVisibility(View.VISIBLE);
} else {
additionalContactFrom.setVisibility(View.GONE);
}
}
});
additionalContactFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"additionalContactFrom clicked!!!", Toast.LENGTH_SHORT)
.show();
if (linearLayoutFrom.getVisibility() == View.GONE) {
linearLayoutFrom.setVisibility(View.VISIBLE);
} else {
linearLayoutFrom.setVisibility(View.GONE);
}
}
});
}
}
in xml file:
<FrameLayout 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" >
<LinearLayout
android:id="#+id/LinearLayoutAdditionalContactFrom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_dark"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayoutFrom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="60dp"
android:layout_marginRight="50dp"
android:background="#android:color/holo_green_light"
android:orientation="vertical"
android:visibility="gone" >
</LinearLayout>
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="48dp"
android:hint="Input if you're not receiver"
android:textAppearance="?android:attr/textAppearanceSmall" />
</FrameLayout>
This is very important that when you want add some view (for example
add a linearlayout to another linerlayout). you should use framelayout or
relativelayout(do not use linearlayout) for do that.
I'm working on an Android application at my work and I am having a problem.
I need to know how I can use the Onchanged and OnTouch methods for the same button with conditions?
For example: If my button is on left than you can only touch it to do something and if it is on right than you can use it as a spinner.
I will suggest to use an image Button for both the case. As your requirement, use image Button as a normal Button when in left-side and spinner when on right. You can make use of a variable "i" to be set if it is in right side. Change the method if it is reset.
<ImageButton
android:id="#+id/imageButtonSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
/>
Then define your spinner in terms of what you need inside it. Like this may be....
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnerLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageButton
android:id="#+id/love"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageButton
android:id="#+id/hem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageButton
android:id="#+id/love"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
In the mainActivity
protected void onCreate(Bundle savedInstanceState) {
...
addListenerToSpinnerButton();
}
private void addListenerToSpinnerButton(){
spinnerButton = (ImageButton) findViewById(R.id.imageButtonSpinner);
spinnerButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
if(p != null)
showSpinner(MainActivity.this, p);
}
});
}
I want to use a custom icon for my pinIt button. I keep getting the old layout overwritten when I run my app while in the preview the icon that I prefer is shown.
<com.pinterest.pinit.PinItButton
android:id="#+id/car_info_comment_pintrest"
android:layout_width="#dimen/car_info_button_width"
android:layout_height="wrap_content"
android:background="#drawable/ic_comment_pintrest"
android:drawableTop="#drawable/ic_comment_pintrest"
android:text="100"
android:textColor="#fff" />
I've fixed it with a hacky solution. I've added another button and called the OnClickEvent of the PinIt button and made that buttons visibility: gone.
holder.mPinterestButton = ((PinItButton) holder.content.findViewById(R.id.pin_it));
holder.mPinterestButton.setImageUrl("http://placekitten.com/400/300");
holder.mPinterestButton.setUrl("http://placekitten.com"); // optional
holder.mPinterestButton.setDescription("A place kitten!"); // optional
holder.pinterestButton = ((Button) holder.content.findViewById(R.id.car_info_comment_pinterest));
holder.pinterestButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.mPinterestButton.performClick();
}
});
XML:
<Button
android:id="#+id/car_info_comment_pinterest"
android:layout_width="#dimen/car_info_button_width"
android:layout_height="wrap_content"
android:drawableTop="#drawable/ic_comment_pinterest"
android:background="#null"
android:text="100"
android:textColor="#fff" />
<com.pinterest.pinit.PinItButton
android:id="#+id/pin_it"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>