I already have the button:
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:drawableLeft="#drawable/empty"
android:id="#+id/buttonMyText"
android:text=" myText"
android:textSize="20px"
android:gravity="left">
</Button>
I have the "empty" icon show on the button when the program starts.
What I want to do is change the button's icon automatically from my code (low, medium and high) based on user inputs
I tried:
Button myButton = bla... bla... bla...
But I cant figure out
myButton.(what?)
If you check the docs, you'll see the code equivalent for each XML attribute.
See here: http://developer.android.com/reference/android/widget/Button.html
Searching for drawableLeft shows:
android:drawableLeft:
setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable)
if you want to change icon at button click event then try this code...
buttonMyText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
buttonMyText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ImageNameHere, 0, 0, 0);
buttonMyText.setTextColor(Color.BLACK);
}
});
Related
I simply wish to change the background color of the button by the value of the text. For example, when I change the text to:
button.setText("YES");
I want to set the background color of the button to green.
and when I change the text to:
button.setText("NO");
I want to set the background color of the button to red.
When I change it in the java code like this:
boolean textValueYES = true;
button.setBackgroundColor(textValueYES ? Color.GREEN : Color.RED);
The button loses its drawable.xml settings.
Is there a way to add this checking to the drawable xml?
Or to set the background color by its text value without losing the drawable settings?
you can create two drawable xml for red and green background color and set that xml programmatically.
button.setBackgroundResource(textValueYES ? R.drawable.green : R.drawable.red);
You have to do like this just write below of setText()
i.e
button.setText("YES");
setBackgroundResource(R.color.green);
and when
button.setText("NO");
setBackgroundResource(R.color.red);
i do like this in my java file
final Button btn_showtouch = (Button)findViewById(R.id.button);
btn_showtouch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if((btn_showtouch.getText()).equals("YES")) {
btn_showtouch.setBackgroundColor(Color.GREEN);
btn_showtouch.setText("NO");
}else if(btn_showtouch.getText().equals("NO")) {
btn_showtouch.setBackgroundColor(Color.CYAN);
btn_showtouch.setText("YES");
}
}
});
}
and your XML file like this
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="YES"
android:id="#+id/button"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="62dp" />
and its worked for me i hope this will help you
I'm creating a compose screen for my app. I have a ScrollView which contains a RelativeView which in turn contains two things: the EditText where the user types a message, and an ImageView whose visibility is toggled on and off depending on whether an image is attached to the status or not. Here's that part of my layout XML.
<!-- #dimen/bigGap = 8dp -->
<ScrollView android:id="#+id/parentScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:layout_marginTop="#dimen/bigGap"
android:layout_marginRight="#dimen/bigGap"
android:layout_marginLeft="#dimen/bigGap"
android:layout_marginBottom="#dimen/bigGap"
android:layout_above="#+id/footer"
android:background="#006400"
> <!-- green background color -->
<RelativeLayout android:id="#+id/parentLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFD700"> <!-- yellow background color -->
<EditText android:id="#+id/postText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#dddddd"
android:inputType="textMultiLine"
android:gravity="top|left"
/> <!-- gray background color -->
<ImageView android:id="#+id/postImage"
android:layout_width="#dimen/thumbnailSize"
android:layout_height="#dimen/thumbnailSize"
android:visibility="gone"
android:layout_below="#id/postText"
/>
</RelativeLayout>
</ScrollView>
Because my EditText's height is wrap_content, the whole thing starts off with a single line of gray background (the EditText) on top of a yellow background (the RelativeLayout, which fully covers the green background of the ScrollView). However, I'll later change all the views' backgrounds to white (to make them look like a single component) and it will be counter-intuitive for the user to be able to tap only that single line of EditText to make the keyboard pop up.
What I want to do is to redirect the click and long click actions of the RelativeLayout to the click and long click actions of the EditText, but my code below doesn't work. Help?
final EditText editText = (EditText) findViewById(R.id.postText);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.parentLinearLayout);
rl.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Logger.d("onClick invoked!");
editText.performClick();
}
});
rl.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Logger.d("onLongClick invoked!");
return editText.performLongClick();
}
});
My intention here is so that when the RelativeLayout is clicked, the keyboard pops up (as it does when done to an EditText) and when long-pressed, display the cut/copy/paste/text selection options (same behavior with EditText).
From the description, What you actually want is to open the keyboard. So your title for the question suggests a solution, not the actual problem.
call this from your click listener (or immediately when you show the page):
((InputMethodManager) myActivity
.getSystemService(Context.INPUT_METHOD_SERVICE))
.toggleSoftInput(InputMethodManager.SHOW_FORCED,
InputMethodManager.HIDE_IMPLICIT_ONLY);
Edit:
You must also call editText.requestFocus(); (#Phil is right), but from my experience it's not enough to open the keyboard, so you'll need also the ugly code above.
What you want is for your EditText to gain focus (which in-turn causes the keyboard to show):
rl.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Logger.d("onClick invoked!");
editText.requestFocus();
}
});
rl.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Logger.d("onLongClick invoked!");
editText.requestFocus();
return true;
}
});
XML
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton1"
android:src="#drawable/image1"
android:onClick="buttonClick"
/>
JAVA
--------------------
public void buttonClick(View v)
{
Button aButton = (Button)v;
aButton.setBackgroundResource(R.drawable.image2);
}
Here's what I've tried so far with no luck...
I want to be able to click the button and change the image to image2, there's also going to be other images i'll change it to based off of other variables. I'm just really stuck.. I'll continue looking at other questions and if I find an answer I'll post it here.
Your buttonClick() needs fixing:
public void buttonClick(View v)
{
ImageButton aButton = (ImageButton)v;
aButton.setImageResource(R.drawable.image2);
}
the View is an ImageButton, not a Button. The src attribute is updated via setImageResource, not setBackgroundResource.
Is there an easy way to use custom images for Zoom Buttons?
I'd like to use default setZoomButtonsVisible functions to manage
show/hide buttons.
How can I override those buttons?
I'd like to use icons from my res/drawable (drawable-hdpi, drawable-ldpi ..)
to make sure images will be good looking on all screens.
You can hide original zoom buttons and enable external zoom:
private XYMultipleSeriesRenderer mRenderer; //or any of other renderer
mRenderer.setZoomButtonsVisible(false);
mRenderer.setExternalZoomEnabled(true);
//then add click events tot he imagebuttons on the view
//mChartView --> private GraphicalView mChartView;
ImageButton btnZoomIn= (ImageButton) findViewById(R.id.btnZoomIn);
btnZoomIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mChartView.zoomIn();
}
});
ImageButton btnZoomOut = (ImageButton) findViewById(R.id.btnZoomOut );
btnZoomOut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mChartView.zoomOut();
}
});
Hope it helps.
The only problem is that some strange think is happening on the click. I posted problem here and also as an issue.
Hope somebody will find an answer.
Toni
I'm using almost the same way I think. I've got:
renderer.setZoomEnabled(true);
renderer.setExternalZoomEnabled(true);
renderer.setApplyBackgroundColor(true);
And I'm using .xml file for my buttons layout. I'm not using <ImageButton> in my code. I'm using <Button> with background. And in this way these buttons are images. My code for button:
<Button
android:id="#+id/zoomin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#drawable/action_zoomin"
android:hapticFeedbackEnabled="true"
android:layout_marginRight="15dp">
</Button>
I wanted the text on my buttons to switch every time that I click it. The only way I know how to switch text is Textswitcher, but I can't seem to find a way to use it with the buttons. I'm sure this is a simple answer and appreciate any help!
XML file portion...
Button
android:id="#+id/Button_A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="#dimen/help_text_size"
android:minWidth="10px"
Java:
Button alphaButton = (Button) findViewById(R.id.Button_A);
alphaButton.setText(mGameSettings.getString(GAME_PREFERENCES_RIGHT, ""));
alphaButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
handleAnswerAndShowNextQuestion("a");
}
});
I'm pretty sure you should just be able to call Button#setText() in the onClick listener