I have few buttons containing only labels. They are made using 2 image resources (pressed is little bit bigger than not pressed)
button_image.png
button_image_pressed.png
and xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_image" android:state_pressed="false"/>
<item android:drawable="#drawable/button_image_pressed" android:state_pressed="true"/>
</selector>
And i want to use custom TextView insted of this.
Is it possible to chage TextView textSize when user touches text and change textSize back when text user releses screen?
I tried different solutions, but they didn't work.
You can try using an onTouchListener().
textView.setOnTouchListener( new OnTouchListener() {
#Override
public boolean onTouch( View v, MotionEvent event ) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
textView.setTextSize( ... );
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
textView.setTextSize( ... );
}
return false;
}
} );
Related
I want to create a simple drop down list/listview like in below image. It should generate programmatically without using any xml layout.
NOTE : I am not using a spinner in here . Also I want to open it when I click on the ImageView next to the Switch.
I have no idea about this .
Have any ideas ?
not perfect, but it works ;)
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(MainActivity.this, button);
popupMenu.getMenu().add("Edit");
popupMenu.getMenu().add("Delete");
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getTitle().toString()) {
case "Edit" :
//execute "edit" action
break;
case "Delete" :
//execute "delete" action
break;
}
return false;
}
});
popupMenu.show();
}
});
Just try to check and implement it
PopupMenu overflowPopupMenu = new PopupMenu(getContext(), finalOverflow);
overflowPopupMenu.getMenuInflater().inflate(R.menu.popup_overflow_options, overflowPopupMenu.getMenu());
overflowPopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(android.view.MenuItem item) {
switch (item.getItemId()) {
case R.id.edit:
break;
case R.id.delete:
break;
}
return true;
}
});
overflowPopupMenu.show();
popup_overflow_options.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="#+id/edit"
android:title="#string/edit"/>
<item
android:id="#+id/delete"
android:title="#string/delete"/>
</menu>
I use PopupMenu's for this. See also this guide. The guide explains how to use the PopupMenu with an xml menu resource.
In your case you would attach a click listener to the ImageView. That listener would then create a PopupMenu using the ImageView as an anchor. Like this: PopupMenu popup = new PopupMenu(imageView.getContext(), imageView);
At this point since you need dynamic menu items you have the following options:
You can call PopopMenu.getMenu() and manually populate it with MenuItems
You can create an xml menu resource and then adjust/hide ones that need to be changed
I have 2 Images:
IMG_1:
IMG_1_PRESSED:
I display IMG_1 by default, but when I click on it, the image should change to IMG_1_PRESSED.
Here's a code snippet of the button itself, where be is an enum containing the correct drawables:
ImageButton ib = new ImageButton(this.context);
ib.setImageResource(be.getDrawable());
ib.setScaleType(ImageButton.ScaleType.CENTER_INSIDE);
ib.setBackgroundColor(Color.TRANSPARENT);
ib.setPadding(0, 0, 0, 0);
ib.setAdjustViewBounds(true);
ib.setMaxWidth(width);
ib.setMaxHeight(height);
setStrengthListener(ib, be);
And here is the setStrengthListener method:
private void setStrengthListener(final ImageButton ib, final ButtonEnum be){
ib.setOnTouchListener(new View.OnTouchListener() {
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
ib.setImageResource(be.getDrawablePressed());
return true;
case MotionEvent.ACTION_UP:
ib.setImageResource(be.getDrawable());
return true;
}
return false;
}
});
}
Now both images are 480x240 and width and height are set to device's width/3 and device's width/6 respectively (I want to display 3 images, hence the /3).
Here's the problem, if the button is pressed, I get this:
There's this little line under the pressed button... I've tried a lot to fix this but somehow that little line has made itself my greatest enemy today.
As an extra effort: the line doesn't show anymore if I set the original resource to IMG_1_PRESSED:
ImageButton ib = new ImageButton(this.context);
ib.setImageResource(be.getDrawablePressed());
ib.setScaleType(ImageButton.ScaleType.CENTER_INSIDE);
ib.setBackgroundColor(Color.TRANSPARENT);
ib.setPadding(0, 0, 0, 0);
....etc
But obviously I don't want to start with a pressed button.
So dear Android experts, what am I doing wrong here?
This is the simplest solution as i know,
Try this
create Xml inside your Drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_pressed_img"android:state_pressed="true" />
<item android:drawable="#drawable/btn_normal_image" />
</selector>
Then add this drawable file as background to your View
Example :
<ImageView
android:id="#+id/enter_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="100dip"
android:maxWidth="100dip"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:background="#drawable/enter_btn" />
or in Java code
yourView.setImageResource(R.drawable.*drawablefile*);
You can use ImageButton also but ImageView would look better than ImageButton since it doesn't have button borders.
So after a hell of a lot of trial and error, I managed to get that grey line away. In the end it was very simple really, though I'm not completely sure why this fixed it:
The main layout:
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setGravity(Gravity.TOP);
The view creation (layout for imageview is unchanged):
ImageView iv = tbb.getTopBarButton(ButtonEnum.IMG_1);
LinearLayout iViewLayout = new LinearLayout(this);
iViewLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
iViewLayout.setOrientation(LinearLayout.HORIZONTAL);
iViewLayout.addView(iv);
ll.addView(iViewLayout);
Adding a separate layout per imageview seemed to remove the grey line. Would love to know WHY this works though, but for future reference, at least I hope to help others.
I am using Android Studio and I was wondering is there any way to add two images to a button and only one of them to be visible(active) at a time?
Now I am using the following technique:
In my activity I have 4 buttons and to all of them I have register an View.OnTouchListener and I manage the events in it like this:
private class ButtonOnTouchListener implements View.OnTouchListener {
#Override
public boolean onTouch(View v, MotionEvent event) {
int eventaction = event.getAction();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
clickButton(v);
return true;
case MotionEvent.ACTION_UP:
unClickButton(v);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
v.callOnClick();
}
break;
}
// tell the system that we handled the event but a further processing is required
return false;
}
private void clickButton(View button) {
switch (button.getId()) {
case R.id.buttonLogin:
button.setBackgroundResource(R.drawable.activity_main_button_enter_press);
break;
case R.id.buttonRegister:
button.setBackgroundResource(R.drawable.activity_main_button_registration_press);
break;
case R.id.buttonCheckUpdate:
button.setBackgroundResource(R.drawable.activity_main_button_update_check_press);
break;
case R.id.buttonExit:
button.setBackgroundResource(R.drawable.activity_main_button_exit_press);
break;
}
}
private void unClickButton(View button) {
switch (button.getId()) {
case R.id.buttonLogin:
button.setBackgroundResource(R.drawable.activity_main_button_enter_idle);
break;
case R.id.buttonRegister:
button.setBackgroundResource(R.drawable.activity_main_button_registration_idle);
break;
case R.id.buttonCheckUpdate:
button.setBackgroundResource(R.drawable.activity_main_button_update_check_idle);
break;
case R.id.buttonExit:
button.setBackgroundResource(R.drawable.activity_main_button_exit_idle);
break;
}
}
}
So in this way I am making a switch in the event to determine which button is clicked and change the appropriate resource. Which means that for each activity I must create a custom class and do the switches in the events.
Could I make something like the following:
private void ClickButton(View button){
button.setBackgroundResource(button.getResourceImage1);
}
private void unClickButton(View button){
button.setBackgroundResource(button.getResourceImage2);
}
This way I can make a global OnTouchListener and just set the images in the xml file for each button and not to worry about any java code.
I tried to switch between background/foreground images but I couldn't hide the foreground image for some reason.
You Can make a drawable file selector for this purpose
Like this:
button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
<item
android:drawable="#drawable/activity_main_button_enter_press"
android:state_pressed="true" />
<item
android:drawable="#drawable/activity_main_button_enter_press"
android:state_selected="true" />
<item
android:drawable="#drawable/activity_main_button_enter_idle" />
And in your java code on click of button:
btn.setSelected(!btn.isSelected());
And set the selector drawable file to your button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_selector.xml"
android:text="Click_me"/>
You could create a State List Drawable for each button and implement android:state_pressed.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
<item
android:drawable="#drawable/activity_main_button_enter_press"
android:state_pressed="true" />
<item
android:drawable="#drawable/activity_main_button_enter_idle" />
</selector>
I have a button, when I click it it makes a scale animation, and also I can drag it with onTouch event.
I want to make it unclickable after first touch and this code does. Here OnClick
btnScale.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
v.startAnimation(animScale);
btnScale.setClickable(false);
}});
Also this code help me to drag it. Here OnTouch
btnScale.setOnTouchListener(new Button.OnTouchListener(){
public boolean onTouch(View v, MotionEvent me) {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
oldXvalue = me.getX();
oldYvalue = me.getY();
} else if (me.getAction() == MotionEvent.ACTION_MOVE) {
//RelativeLayout rl = (RelativeLayout) findViewById(R.id.background);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(v.getWidth(), v.getHeight());
params.leftMargin = (int) (me.getRawX() - (v.getWidth() / 2));
params.topMargin = (int) (me.getRawY() - (v.getHeight()));
//LayoutParams params = new LayoutParams(v.getHeight(), (int) (me.getRawX() - (v.getWidth() / 2)), (int) (me.getRawY() - (v.getHeight())));
v.setLayoutParams(params);
}
return false;
}});
However I still want to drag it after I click it. However when I make it unclickable it cannot be dragable since it is unTouchable too
EDIT:
When I use if else state on onClick as boolean variable, I still able to click the button and a ugly square appears in my circular button.
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#61d5ee">
<item>
<shape android:shape="oval">
<solid android:color="#61d5ee"/>
<corners android:radius="5dp" />
<stroke android:width="4px" android:color="#cdf3fb" />
</shape>
</item>
</ripple>
What to do?
Create a boolean to say if it is enabled or not, change manually the state in your onClick and change the button's design too. In your Onclick use something like:
if(enabled){
do something
}else{
//do nothing
}
You don't need the else, it's just an example.
Then... he is touchable enabled and disabled.
I have been trying to figure out why my boolean is not changing when I press the button, when I changed it manually it worked, but it doesn't do any thing. I have tried to follow tutorials to the word but they don't work. Can anybody point out where I am going wrong?
public boolean onOptionsItemSelected(MenuItem menu)
{
MenuItem freeze = (MenuItem)findViewById(R.id.freeze);
// Handle item selection
switch (menu.getItemId()) {
case R.id.freeze:
if (freze == false){
freze = true;
} else {
freze = false;
}
return true;
case R.id.toggleVolCount:
if (toggleVol == true){
toggleVol = false;
} else {
toggleVol = true;
}
return true;
default: return super.onOptionsItemSelected(menu);
}
Thanks for all your help, when I tried the code that was suggested and it didn't work I went back and changed the menu. Previously I had made a button with an onClick to create the menu, when created the icon with code the code that I had previously written worked fine. Hope this helps someone other than me so I don't feel like so much of an idiot.}
In res folder create one folder menu like drawable
Create new xml file optionmenu.xml in that folder.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menuitem"
android:title="Prefs">
</item>
<item android:id="#+id/menuitem1"
android:title="Prefs1">
</item>
</menu>
In onCreate method write this code....
setOptionMenu(R.menu.optionmenu);
and in overide method of Menu write this code.....
#Override
public boolean onOptionsItemSelected(MenuItem menu) {
switch (menu.getItemId()) {
case R.id.menuitem:
startActivity(new Intent(this, Prefs.class));
break;
case R.id.menuitem1:
startActivity(new Intent(this, Prefs1.class));
break;
default:
break;
}
return true;
}