I want to have a button with a 3D effect, so if you press it, it looks like the button is literally pressed down (inside the 'ground').
Like this:
But i want to save file size and only save the background texture without the text on it. So i am using TextButtons and tried to detect if the button is 'held down' like this:
tbs1 = new TextButton.TextButtonStyle();
tbs1.up = new TextureRegionDrawable(Game.res.getAtlas("buttons_3").findRegion("button3_green_normal"));
tbs1.down = new TextureRegionDrawable(Game.res.getAtlas("buttons_3").findRegion("button3_green_pressed"));
tbs1.font = FontManager.bignoodle_90;
quickMatchButton = new TextButton("Quick Match", tbs1);
quickMatchButton.getLabelCell().padBottom(ASR.h(50));
quickMatchButton.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
Game.gsm.setState(Game.gsm.CONFIGUREMATCH);
}
});
quickMatchButton.addListener(new ClickListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button){
//This is not working!
quickMatchButton.getLabelCell().padBottom(ASR.h(100));
return true;
}
});
The problem is that (i think) i can't change the position of the Text Label inside the button after i added the button to my layout table.
So main question: How can i change the position (or the padding) of the TextLabel inside my button, after i created the button?
Thanks for your time!
you can try this:
tbs1.pressedOffsetY = -3;
this works for ImapgeButton, as pressedOffsetY belongs to ButtonStyle, it should work with text buttons too.
Related
I have certain entries in my list view item. There I have a simple "like button" (not facebook like button). You can see the above mentioned SCREENSHOT; for the reference.
The moment I click on like button; i want the like button color to be changed and the like button color should remain same(changed on like) when I'll login again.
Also, all the entries must get filled in Database with cust_id, bus_id, Offer_id using json; that I know very well.
When I again click on the same button(like button), whose color has been changed. It must be changed back to the default color and data must get removed from database.
How can I do this...?
1. How to get value of click button.
2. How to bring back the changed color to default; once the button has been re-clicked.
Plz suggest me...
this is button code
holder.b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (clicked) {
holder.b1.setBackgroundResource(R.drawable.like_icon_hover);
} else {
holder.b1.setBackgroundResource(R.drawable.like_icon);
}
clicked = true;
}
});
You need to add a listener to the button and using ValueAnimator you can change the button color and reverse it back when you click again.
Here is a simple and best approach to achieve your scenario. Add the onClick listener for the button in your list item like this.. I have explained each line ..
// set a default background color to the button
placeHolder.likeButton.setBackgroundColor(Color.RED);
placeHolder.likeButton.setOnClickListener(new View.OnClickListener() {
ValueAnimator buttonColorAnim = null; // to hold the button animator
#Override
public void onClick(View v) {
// first time this will be null
if(buttonColorAnim != null){
// reverse the color
buttonColorAnim.reverse();
// reset for next time click
buttonColorAnim = null;
// add your code here to remove from database
}
else {
final Button button = (Button) v;
// create a color value animator
buttonColorAnim = ValueAnimator.ofObject(new ArgbEvaluator(), Color.RED, Color.BLUE);
// add a update listener for the animator.
buttonColorAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animator) {
// set the background color
button.setBackgroundColor((Integer) animator.getAnimatedValue());
}
});
// you can also set a delay before start
//buttonColorAnim.setStartDelay(2000); // 2 seconds
// start the animator..
buttonColorAnim.start();
// add your code here to add to database
}
}
});
This will change the button color on your first click and then revert the color back on the next click. You can also set a delay to change the color.
Note: You have to set the default button color based on your logic.
#Override
public void onClick(View view) {
if(!check)
{
personViewHolder.img_like_job.setImageResource(R.drawable.ic_thumbsup_blue);
check = true;
}
else
{
personViewHolder.img_like_job.setImageResource(R.drawable.ic_thumbsup);
check = false;
}
}
you can use custom adapter for your listview(it has own layout.xml),and you can set your clicklistener in it.
You can change color or what you want. Actually I did have project like you want.I put some link if you can t do it.
Try following:
use setOnClickListener() on the button.
eg.
viewHolder.imgVwFbLike.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
// TODO :
// 1. make webservice call to update like status (Assuming a web service call)
// 2. Implement a callback for webservice call, to get the status of request.
if(success)
a) change the colour of like btn. and insert the data in Db.
b) Also maintain a column in db for likestatus(by default set it false).
}
}
);
Assuming you are fetching the data from db when you login, you can check the likestatus and set the color of button accordingly.
I have put a lot of buttons (16*16) in a button array.
The buttons numbers are directly related to changes they should do in another array (e.g. button[12][7] sets the value of stat[12][7] to 1)
So I thought it's possible to put on single line in the onTouch method that reacts to every button.
Example (of course, not working)
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
if(arg0 == button[int a][int b]){stat[a][b]=1};
In this pseudocode, the button would create 2 ints that describe the 2 dimensions of the array which get passed to the stat array.
If anyone had a solution for this, he would save me a few hours this night.
Thanks for your answers.
I think HasMap is a better solution
private HashMap<Integer,Button> btnMap = new HashMap<Integer, Button>();
private void init(){
Button yourFirstBtn = (Button) findViewById(R.id.yourFirstBtn);
btnMap.put(yourFirstBtn.getId(), yourFirstBtn);
for(Button tempBtn: btnMap.values()){
tempBtn.setOnClickListener(this);
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button clickedBtn = btnMap.get(v.getId());
}
Are you adding the onTouchListener to the buttons' container?
Your best bet is to add an onTouchListener to each button, and then arg0 will correspond to the specific button.
Another option would be to use a GridView, which has an setOnItemClickListener you can use. http://developer.android.com/reference/android/widget/GridView.html
As you add each button to the array, set a tag that indicates its indices. Tags are there for adding properties to a view without having to resort to another data structure.
For example:
button[12][7].setTag("12|7");
If your button were pre-defined in XML, you could do the same with:
android:tag="12|7"
Then in the touch listener (I assume the same one is attached to all the buttons), get the tag from the view that was touched:
String tag = (String) view.getTag();
Then substring out and use the two indexes as required:
String indx1 = tag.substring(0, tag.indexOf("|"));
String indx2 = tag.substring(tag.indexOf("|")+1);
stat[Integer.parseInt(indx1)][Integer.parseInt(indx2)] = 1;
Try something like this:
Button[][] myButtonMatrix = new Button[] {
new Button[] { button11, button12, button13, button 14 },
new Button[] { button21, button22, button23, button24 }
};
public class MatrixButtonListener implements View.OnClickListener {
private int x;
private int y;
public MatrixButtonListener(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() { return x; }
public int getY() { return y; }
#Override
public void onClick(View v) {
stat[x][y] = x-y; // changes were made only in relation to x and y, nothing else
// for example:
if(x == 0) {
// button in first row
// do something
}
}
};
// to apply to each button in matrix:
for(int i=0; i<myButtonMatrix.length; i++) {
for(int j=0; j<myButtonMatrix[i].length; j++) {
myButtonMatrix[i][j].setOnClickListener(new MatrixButtonListener(i,j));
}
}
What this is supposed to do:
Create a generic OnClickListener class, which takes the x and y position as parameter, so each onClickListener has the same behaviour, but differnet x and y positions, depending on the button itself.
Note: This is not tested.
EDIT:
Another way would be a custom button class, which you use, which contains the X/Y coordinates as well. Simply add onClickListener to each button, cast it back to your custom view, and ask for x/y.
Change the color of the two buttons when its correct
I have a game, comparing buttons from the left side and the right side.
when you click on the left side and compare it to right side and its correct the 2 buttons must be color green. How can i implement it?
public void getClick1(int num)
{
firstClick = arrNum1[num];
}
public void getClick2(int num){
secondClick = arrNum2[num];
if (firstClick == secondClick){
guess.setText(Integer.toString(--score ));
}else{
Toast.makeText(this,"WRONG!", Toast.LENGTH_LONG).show();
}
}
You can change the color of the Button with this method:
btn2.setBackgroundColor(Color.RED);
... where btn2 is the instance of Button.
I have a requirement of flashing the original word for elliptic one(which is shorted because of space constraint around it) if there is hovering done by S-pen on the particular part of that screen.
I have the code of finding the hovering position and finding the elliptic word as well.
I want a sample code to pass the Exact word for the elliptic word so that I can show the original word once pen is pointing that particular position.
(Since its newly introduced in Android with JB I am not finding much help from google..)
You should use PopupWindow. Create a XML layout containing TextView, inflate it within the pop-up window (or just create it through constructor).
public class HoverPopup extends PopupWindow {
public HoverPopup(Context context, String text) {
//...
TextView textView = (TextView) View.inflate(context, R.layout.hover_popup, null);
textView.setText(text);
setContentView(textView);
}
}
S Pen event library initialization and the hover listener implementation
private final SPenEventLibrary mSPenEventLibrary = new SPenEventLibrary();
private final SPenHoverListener mSPenHoverListener = new SPenHoverListener() {
private HoverPopup mHoverPopup;
#Override
public boolean onHover(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_HOVER_ENTER:
mHoverPopup = new HoverPopup(mContext, "Your text");
mHoverPopup.showAsDropDown(view);
return true;
case MotionEvent.ACTION_HOVER_EXIT:
mHoverPopup.dismiss();
return true;
default:
return false;
}
}
};
Do not forget to attach the S Pen hover listener to a view
mSPenEventLibrary.setSPenHoverListener(view, mSPenHoverListener);
See sample code of Spen SDK by Samsung
I have 3 components on my application, 1 textview(inputType:textMultiline, scrollbar:vertical, gravity:bottom|right) at the top, 1 editview at the middle and 1 button at the bottom. When I type something on my editview and I click the button, the text written on the edit view displays on the textview. Every time i hit ok, the text displays the the bottom and what is displayed is the first three inputs. I have to scroll down the textview in able for me to see my last input.
Now, I want my users to see their last input on their textview. I want to know if there is such code for auto scrolldown for textviews everytime I input a new text on it. I am new on developing android apps. Thanks!
When you set text to textview just set foucus to it. like
tv.setFocusable(true);
It will automatically focus your view whenever you change your string on textview.
If you are adding text to your text view again and again then you can try this
int scroll_amount = tv.getBottom();
tv.scrollTo(0, scroll_amount);
Hope it will work not sure..
Try this also
int scroll_amount = (int) (tv.getLineCount() * tv.getLineHeight()) - (tv.getBottom() - tv.getTop());
tv.scrollTo(0, scroll_amount);
you can use scrollView.scrollTo(x,y) to auto scroll to position that you want.
/*edit*/
create custom class for scrollview
package com.android.mypackage
public class myScrollView extends ScrollView{
private int maxY = 0;
#Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
super.onScrollChanged(x, y, oldx, oldy);
if(y>maxY)
maxY=y;
}
public void moveToEnd(){
this.scrollTo(0, maxY);
}
}
using this custom class in layout xml as below:
...
<com.android.mypackage.myScrollView
android:id="#+id/my_scrollview"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView .../>
</com.android.mypackage.myScrollView>
.....
when you press Ok button just call function myscrollViewObj.movetoEnd();
it's just a draft code, still not tested.
I already got the answer for this! Thank you for giving me some idea. I might be able to use them in the future. #Bharat Sharma, you almost got the answer! Thanks!
public void onClick(View v) {
// TODO Auto-generated method stub
log.setText(log.getText() + "\n" +input.getText());
if(log.getLineCount() > 5){
scroll_amount = scroll_amount + log.getLineHeight();
log.scrollTo(0, scroll_amount);
}
}
I called the variable scroll amount outside the onCreate(). Thanks again!
This is it.
public void onScrollDownClicked(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mTextView.scrollTo(0, mTextView.getLayout().getHeight() - mTextView.getHeight());
}
}