Android Button Not moving onclick - java

I have an up button which I want to move up onClick but when I click on it it moves way to far and gets to the end of the screen and then shrinks down untill you cant see it. It moves up too much, but why? I only increase it by one unit?
final Button upbutton = (Button)findViewById(R.id.upbutton);
upbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams)
upbutton.getLayoutParams();
mParams.bottomMargin += 1;
upbutton.setLayoutParams(mParams);
}
});
}

Because you're not assigning mParams to your buttons params.
mParams = upbutton.getLayoutParams();

You seem to be increasing parameters for your RelativeLayout then assigning those to your button. So the button gets confused. Try looking for a set margin option or something on the actual button view.

Related

Floating Action bar menu appears across every fragments

I am using an library called CircularFloating to show menu in my homepage in one fragment but when i click and go to the next fragmetn it still appears to be in next fragment also. how can I remove or set visibility off when I move to next fragments.
here is my code.
SubActionButton.Builder itemBuilder = new SubActionButton.Builder(getActivity());
ImageView itemIcon1 = new ImageView(getActivity());
itemIcon1.setImageDrawable(getResources().getDrawable(R.drawable.camera_button));
SubActionButton button1 = itemBuilder.setContentView(itemIcon1).build();
ImageView itemIcon2 = new ImageView(getActivity());
itemIcon2.setImageDrawable(getResources().getDrawable(R.drawable.button_action_dark_touch));
SubActionButton button2 = itemBuilder.setContentView(itemIcon2).build();
final FloatingActionMenu actionMenu = new FloatingActionMenu.Builder(getActivity())
.addSubActionView(button1)
.addSubActionView(button2)
.attachTo(actionButton)
.build();
itemIcon2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
Fragment abt = new Feedback_Fragment();
((AppCompatActivity)getActivity()).getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, abt).addToBackStack(null).commit();
actionMenu.close(true);
}
});
Library I have used is
implementation 'com.oguzdev:CircularFloatingActionMenu:1.0.2'
You can remove the visibility with
actionMenu.setVisibility(View.INVISIBLE)
or
actionMenu.setVisibility(View.GONE)
And set the visibility to visible again with
actionMenu.setVisibility(View.VISIBLE)
Without knowing exactly how this library is built, you could try playing around with the attribute
app:elevation="0". Try to set this attribute to the layout that is currently appearing on top of all fragments, if this attribute is available for that layout. If not, try setting it for the FloatingActionButtons. Hope this helps.

How do I display a Random number generated in a TextView?

I am generating random flashes of a button and I want to know what exact number is being chosen each time so I can eventually add them to an ArrayList for a project I am undergoing.
TextView randomTextView;
Random r = new Random();
private void sequenceFunction() {
//Change Alpha from Fully Visible to Invisible
final Animation animation = new AlphaAnimation(1, 0);
//Duration - A Second
animation.setDuration(1000);
//Animation Rate
animation.setInterpolator(new LinearInterpolator());
animation.setStartOffset(250);
animation.setDuration(250);
//Repeat Animation
animation.setRepeatCount(r.nextInt(10));
// Reverse animation at the end so the button will fade back in
animation.setRepeatMode(Animation.REVERSE);
//Button 1 Flashes
final Button btn = (Button) findViewById(R.id.button);
btn.startAnimation(animation);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
view.clearAnimation();
}
});
}
I want to display the result of what ever random number has been generated via the randomTextView TextView. This part is critical so that I know that the random function is working as it is supposed to. I have already tried
randomTextView.setText(r.nextInt(10));
However it didn't like it. Any ideas on how to get the random number selected would be greatly appreciated ?
Hope this helps -
private void sequenceFunction() {
//Change Alpha from Fully Visible to Invisible
final Animation animation = new AlphaAnimation(1, 0);
//Duration - A Second
animation.setDuration(1000);
//Animation Rate
animation.setInterpolator(new LinearInterpolator());
animation.setStartOffset(250);
//Repeat Animation
int randomValue = r.nextInt();
// code to add value to array
animation.setRepeatCount(randomValue);
randomTextView.setText(String.valueOf(randomValue));
// Reverse animation at the end so the button will fade back in
animation.setRepeatMode(Animation.REVERSE);
//Button 1 Flashes
final Button btn = (Button) findViewById(R.id.button);
btn.startAnimation(animation);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
view.clearAnimation();
}
});
}
If you have look in the documentation of TextView, you can see that setText(int) actually expects a resource id: https://developer.android.com/reference/android/widget/TextView.html#setText(int)
So you have to turn your int to a CharSequence, you could simply do
randomTextView.setText("" + r.nextInt(10));
which converts your integer to a string.
TextView.setText can be used with either a resource ID (which is an integer) or with a CharSequence, for example a String.
There are many ways to do this, including
int random = r.nextInt(10);
randomTextView.setText(String.valueOf(random);
randomTextView.setText(Integer.toString(random);
randomTextView.setText(String.format("%d", random);
Do not use "" + r.nextInt(10). I know, it is short, it is handy, but it is simply inefficient and bad style.

Choosing database using radio buttons

I'm teaching myself to write an Android app. I use the following code to successfully choose the different database for my app:
public void manageDB()
{
setContentView(R.layout.dbmanager);
ScrollView ll = (ScrollView) findViewById(R.id.lstDb);
final RadioGroup rg = new RadioGroup(this);
rg.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.FILL_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT));
rg.setOrientation(RadioGroup.VERTICAL);
for (int i=0; i < mDBList.items.size(); i++)
{
RadioButton rb = new RadioButton(this);
rb.setId(VIEW_RADIO_ID + i);
rb.setText(mDBList.items.get(i).dictionaryName);
rb.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
rg.addView(rb,i);
if (mDBFile != null && mDBFile.fileName.equals(mDBList.items.get(i).fileName))
{
rg.check(VIEW_RADIO_ID + i);
}
}
ll.addView(rg);
Button btnOk = (Button) findViewById(R.id.btnOk);
btnOk.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
setContentView(R.layout.main);
int selectedIndex = rg.getCheckedRadioButtonId();
if (selectedIndex < 0)
{
selectedIndex = 0;
}
mDBFile = mDBList.items.get(selectedIndex - VIEW_RADIO_ID);
savePreferences();
setContentView(R.layout.main);
menuMain();
}
});
}
With this code, I can click on a radio button to choose a database, and the click on the OK button to confirm and return to the main screen. Now I want to assign a button on the main screen to do this task so that I don't have to leave the main screen when choosing a database.
Since I have two pieces of database, ideally I want a two-way button to do this task. I mean when I click on the button, db_1 is chosen. When the button is clicked again db_2 is chosen, and vice versa.
I have no idea how to adapt this code to meet this requirement of mine. I wonder if you guys can give me a little help. Thank you very much in advance.
If your need is to change database in one click instead of two, you could use a radiogroup containing your 2 radiobuttons, and listen for checked radiobutton change.
Then in onCheckedChange() you load the right database.
Why don't you use "Switch" I think it will solve your purpose. Look at the link [here:][1]
[1]: http://developer.android.com/reference/android/widget/Switch.html Or you can also use the toggle buttons. Hope it helps.

setBackgroundDrawable works correctly; until another button is pressed, why?

In my app I have 4 main buttons, plus another 2. These 4 buttons are declared at the beginning of the main activity.
Button button1, button2, button3, button4;
button1 = (Button) findViewById(R.id.button1);
button1.setTag("blue");
(each button has a tag and is set the same way as button1)
The four buttons I want to cycle through different colours when they are pressed. I manage this by;
public void button1(View v) {
if ("blue".equals(button1.getTag())) {
button1.setBackgroundDrawable(getResources().getDrawable(
R.drawable.brown));
button1.setTag("brown");
} else if ("brown".equals(button1.getTag())) {
button1.setBackgroundDrawable(getResources().getDrawable(
R.drawable.red));
button1.setTag("red");
} else if //...etc
This works all well and good until I press any of the two buttons, an example code of one of the buttons
public void back(View v) {
setContentView(R.layout.main);
t = new TextView(this);
t = (TextView) findViewById(R.id.textView1);
t.setText("");
}
Once I press any of the two buttons the colours change back to the original drawable set in the xml file
android:background="#drawable/blue"
Now when I press the 4 main buttons the drawable does not change, but I definitely know that it is getting re-tagged, so why won't it change the drawable after I press the button?
If your 'two buttons' onClick handler makes a call to Activity.setContentView(int), then all of the buttons will reset to how they are specified in the original XML layout. New views will be inflated and these will not have a tag (you do not seem to be re-setting the tags after the call to setContentView). A null tag will not match any of your colour strings and so your buttons will not cycle their background.
If you want to maintain the views how they were, then do not reset the content view of the Activity. In most cases, setContentView is only called once per lifetime of an Activity, although obviously there can be a few exceptions.

Dynamically adding Buttons in Android doesn't work after the first Button is added

I have a button that I have created in code, which has a listener for Click events. Every time that the button is clicked, it should generate another button and add it below the original button. However, no matter how many times I click the first button, it will only add a dynamic button once, and not add any more.
Here is my coding:
public class DynaminControlActivity extends Activity {
private RelativeLayout container;
private int mainIdCnt = 0;
private int mainId = 100;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
createMainButton();
}
public void createMainButton() {
container = (RelativeLayout) findViewById(R.id.workLayout);
Button b = new Button(this);
b.setId(mainIdCnt + mainId);
CharSequence text = "Main +";
b.setText(text);
container.addView(b);
if (mainId > 0) {
mainId++;
}
b.setOnClickListener((new View.OnClickListener() {
public void onClick(View v) {
createDynamicButton();
}
}));
}
public void createDynamicButton() {
container = (RelativeLayout) findViewById(R.id.workLayout);
Button b = new Button(this);
CharSequence text = "Main +";
b.setText(text);
RelativeLayout.LayoutParams relLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relLayout.addRule(RelativeLayout.BELOW, mainIdCnt + mainId);
container.addView(b, relLayout);
if (mainId > 0) {
mainId++;
}
}
A few things...
If your main layout is a LinearLayout, you shouldn't need to add a rule to indicate that the button should appear underneath the existing button - it will automatically be added to the very bottom (vertical alignment) or very right (horizontal alignment) of the layout.
All your buttons have the same text. Are you certain that you're clicking the first button each time? I note that only your first button has a listener on it, so if you're accidentally clicking one of the other buttons then nothing will happen.
If you're intending to add multiple buttons, it will quickly expand to be larger than the screen size, so you should make sure that your main layout is within a ScrollView so that you can see all the buttons you add
The call to setId() might be stuffing around with the internal workings of Android. Rather than setting an ID, you should let Android generate the ID automatically, and just retrieve that value if you need to reference it.

Categories