How to put dynamic button using xml.
i want to fetch the position of button from the xml and place them in screen.
What do you mean by dynamic?
create an xml file called filename.xml, put this in. Change the drawables where necessary. That is bt_return and bt_returnpress
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bt_return" android:state_pressed="false" />
<item android:drawable="#drawable/bt_returnpress" android:state_pressed="true" />
<item android:drawable="#drawable/bt_return" android:state_focused="false" />
<item android:drawable="#drawable/bt_returnpress" android:state_focused="true" />
</selector>
Then assign the filename.xml to the background of the dynamic button u want.
If I'm not wrong you want to add a button dynamically in your view pragmatically?
If you have to get the parent view in which you want to add button, then create a button and add.
For example you have a LinearLayout already defined.
LinearLayout controls =
(android.widget.LinearLayout)
findViewById(R.id.id_of_your_layout);
Button button = new Button(context);
controls.add(button);
that's it.
In your xml layout file:
<LinearLayout android:id="#+id/layoutbase"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
Then in your code:
LinearLayout layoutbase =
(LinearLayout) findViewById(R.id.layoutbase);
LinearLayout.LayoutParams mylayout = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button mybutton = new Button(this);
mybutton.setText("Button!");
mybutton.setTag("mybutton");
mybutton.setId(999);
mybutton.setLayoutParams(mylayout);
mybutton.setOnClickListener(this);
You also need to implement the OnClickListener:
#Override
public void onClick(View v) {
String tag = (String) v.getTag();
if (tag == "mybutton") {
// do some stuff
}
}
OR
#Override
public void onClick(View v) {
switch (v.getId()) {
case 999:
// do some stuff
break;
default:
}
}
Related
This is inside a custom view class that extends RelativeLayout not inside an Activity or Fragment.
My RadioGroup exists in my xml and I'm trying to dynamically add RadioButtons to it. The only time I've seen it where the mutual exclusivity does not work, is if you do not add an id but I am here. What happens is when I tap a radio button more than 1 button is selected at a time. What am I missing here?
for(String buttonName : mButtonNames){
RadioButton radioButton = new RadioButton(getContext());
radioButton.setId(ViewCompat.generateViewId() );
radioButton.setText(buttonName);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.weight = 1;
radioButton.setLayoutParams(layoutParams);
radioButton.setBackground(mRadioBckgrdDrawable);
radioButton.setButtonDrawable(null);
radioButton.setGravity(Gravity.CENTER);
radioButton.setTextSize(mTextSize);
radioButton.setTextColor(mTextColor);
mRadioGroup.addView(radioButton);
}
Background drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected-->
<item android:drawable="#drawable/two_stage_toggle_button_on"
android:state_checked="true" />
<!-- When not selected-->
<item android:drawable="#drawable/two_stage_toggle_button_off"
android:state_checked="false"/>
</selector>
I dynamically generate TextViews which work like buttons. Now i want to highlight them when they get pressed. Something like change Text color or background color.
I have tried to use selector, but it doesn't work.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#ffffff"/>
<item android:state_pressed="true" android:state_enabled="false" android:color="#ffffff" />
</selector>
Here my loop for creating the TextViews.
int z = 0;
for (MOKGenericDataItem d : data) {
if (d.getButtonText() != null) {
final int pagePosition = z;
TextView btn = new TextView(getActivity());
btn.setId(z);
final int id_ = btn.getId();
btn.setText(d.getButtonText());
btn.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
btn.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
btn.setGravity(Gravity.CENTER);
mLineareLayoutViewPagerButtons.addView(btn);
btn1 = ((TextView) view.findViewById(id_));
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mViewPager.setCurrentItem(pagePosition,false);
}
});
}
z++;
}
First of all your this line is creating ambiguity as you are taking a variable name as btn1 (which is relating it to button)and you are taking a reference of TextView,
btn1 = ((TextView) view.findViewById(id_));
Anyways,Go step by step,
create an xml like label_bg.xml like the following in the drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pressed_color"
android:state_pressed="true" />
<item android:drawable="#drawable/normal_color" />
</selector>
In values folder create another xml like the following,named as labelcolors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="pressed_color">#7ec0ee</drawable> <!--custom color for pressed state -->
<drawable name="normal_color">#00FFFFFF</drawable> <!--transperent color for normal state -->
</resources>
Now set the background of the label as label_bg.xml
<TextView
android:id="#+id/yourlabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="760dp"
android:layout_marginTop="515dp"
android:background="#drawable/label_bg" <!--like this-->
android:text="LabelText"
android:textSize="20dp" />
as you are dynamically adding the views so you need to set the background for your each textView programatically .For that call setBackgroundResource() on the textview object created and set label.xml as background
You need to create a class implements with OnTouchListener and Detect touch Motin. ACTION_DOWN, change text color and ACTION_UP change it's default color according to your requirement.
Code:
public class CustomTouchListener implements View.OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
((TextView) view).setTextColor(0xFFFFFFFF); // white
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
((TextView) view).setTextColor(Color.parseColor("#4a4a4a")); // lightblack
break;
}
return false;
}
}
Now set TouchListener using
textView.setOnTouchListener(new CustomTouchListener());
I want to custom button ,
If user pressed it will show red color and still show red until user pressed other button
how to do this? thanks.
Try this code:
final Button b1 = (Button)findViewById(R.id.btn_1);
final Button b2 = (Button)findViewById(R.id.btn_2);
b1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
b1.setBackgroundColor(Color.RED);
b2.setBackgroundColor(Color.WHITE);
}
});
b2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
b2.setBackgroundColor(Color.RED);
b1.setBackgroundColor(Color.WHITE);
}
});
Use OnClickListeners to change the background of the Button using setBackgroundColor() or setBackgroundDrawable()
You can use a CheckBox for your button and set the background to a state list drawable that tests for the android:state_checked attribute.
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListeners(new OnClickListener() {
public void onClick(View v) {
button1.setBackgroundColor(Color.RED);
}
});
button2.setOnClickListeners(new OnClickListener() {
public void onClick(View v) {
button1.setBackgroundColor(Color.WHITE);
}
});
What you want is a state list. A quick Google found this article: http://blog.androgames.net/40/custom-button-style-and-theme/ that explains them step by step. That way you don't need any code :)
Here's a code that you need to save as an .xml file and place into your drawable folder.
The android:drawable tags point to the drawable resources for each button state. You can shorten this list if you want to.
Then you can use it as a drawable when creating your layout.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/menu_button_normal" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/menu_button_normal" />
<item android:state_pressed="true" android:drawable="#drawable/menu_button_pressed" />
<item android:state_enabled="true" android:drawable="#drawable/menu_button_normal" />
<item android:state_enabled="false" android:drawable="#drawable/menu_button_normal"/>
</selector>
I'm not sure this is even possible. I have this button:
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="45px"
android:drawableTop="#drawable/buttontv"
android:layout_weight="1"
android:background="#null"
android:textColor="#ffffff"
android:textSize="9px"
android:text="TV"/>
And this button has this item xml:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/tv" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/tv_pressed" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/tv_pressed" />
<item
android:drawable="#drawable/tv" />
</selector>
And in my application I use this code for when clicking my button:
OnClickListener b1Listener = new OnClickListener(){
#Override
public void onClick(View v) { loadUrl("http://example.org/"); v.setPressed(true); }
};
Button b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(b1Listener);
What I would like that when I have pressed the button, the drawableTop sets to one of the items with the #drawable/tv_pressed attribute value - and stays there (as in 'this is the currently active button').
I tried adding v.setPressed(true) in the onClick function (as this was all I could find with Google) but that didn't work.
Can this be done? Or is there a better alternative to what I'm trying to accomplish?
If you need a button that gets pressed and stays active, use a ToggleButton
https://stackoverflow.com/a/9750125/1257369
button.setFocusable(true);
button.setFocusableInTouchMode(true);///add this line
or with styles.xml
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
as mentioned above by the answer, it is ideal to use a togglebutton but if you are using a listview in this scenario, then the togglebutton will block out your listview's setOnItemClickListener. You can fix that issue as well by adding the descendants as blocked in the listview layout. However some people have reported that even then, their listview items won't click after using toggle buttons.
Are you working with dialogs/dialogfragments?
This code might help you if you are, because i had a simmilar problem and this worked for me. :)
1- extend your class with DialogFragment
,Override Onstart() method.
#Override
public void onStart() {
super.onStart();
final AlertDialog D = (AlertDialog) getDialog();
if (D != null) {
Button positive = (Button) D.getButton(Dialog.BUTTON_POSITIVE);
positive.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (edittext.equals("")) {
Toast.makeText(getActivity(), "EditText empty don't close",Toast.LENGTH_SHORT).show();
} else {
D.dismiss(); //dissmiss dialog
}
}
});
}}
Is there a way to specify an alternative background image/color for a Button in the XML file that is going to be applied onClick, or do I have to do a Button.setBackground() in the onClickListener?
To change the image by using code:
public void onClick(View v) {
if(v.id == R.id.button_id) {
ButtonName.setImageResource(R.drawable.ImageName);
}
}
Or, using an XML file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/login_selected" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/login_mouse_over" /> <!-- focused -->
<item android:drawable="#drawable/login" /> <!-- default -->
</selector>
In OnClick, just add this code:
ButtonName.setBackgroundDrawable(getResources().getDrawable(R.drawable.ImageName));
In the latest version of the SDK, you would use the setBackgroundResource method.
public void onClick(View v) {
if(v == ButtonName) {
ButtonName.setBackgroundResource(R.drawable.ImageResource);
}
}
public void methodOnClick(View view){
Button.setBackgroundResource(R.drawable.nameImage);
}
i recommend use button inside LinearLayout for adjust to size of Linear.
Try:
public void onclick(View v){
ImageView activity= (ImageView) findViewById(R.id.imageview1);
button1.setImageResource(R.drawable.buttonpressed);}
I used this to change the background for my button
button.setBackground(getResources().getDrawable(R.drawable.primary_button));
"button" is the variable holding my Button, and the image am setting in the background is primary_button