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.
Related
I'm working on android app that creat layout with textView & checkbox programmatically for each text user input in EditText, and when the user select one of the checkbox and click on delete button the layout that contain that checkbox remove from the main layout
public void plusBtn(View view)
{
item = actv.getText().toString(); // text from EditText
actv.setText("");
creatt();
}
public void deletBtn(View view)
{
if(chbox.isChecked()){
linear.removeView(linr);
}
}
public void creatt()
{
linr = new LinearLayout(this);
linr.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
linr.setOrientation(LinearLayout.HORIZONTAL);
linr.setGravity(Gravity.RIGHT);
TextView txt = new TextView(this);
txt.setText(item);
linr.addView(txt);
chbox = new CheckBox(this);
linr.addView(chbox);
linear.addView(linr); // main layout
}
But when I click on delete button just the last layout removed, and that not what I want.
though the technique you're using is not fine. but let me give you a shortcut. in your deletBtn method try to get the reference of the desired deleted view (try to handle case of id or boolean you've to make logic) and enclose this line linear.removeView(linr); into that condition.
Summery, only delete the view which meets the condition.
Alternative, do your whole task from scratch with the help of Recycler-view. as it Recycler-view will give you exact location of cell.
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.
I'm trying to write an application that’s adding buttons dynamically.
The application receives an image from the server and adding a button (no functionality for now).
The main activity is holding linear layout which will contain the buttons and a surface view that would receive the images and would create the buttons in the main activity.
The problem is that after the first image has been received the application crash while trying to add a new button to the view.
This is the surface view code:
public void run() {
while ( isRunning){
if ( !ourHolder.getSurface().isValid()){
continue;
}
Canvas canvas = ourHolder.lockCanvas(); // Semafor for the canvas
canvas.drawRGB(20,20,80);
if (getNumOfBoards() > 0){
canvas.drawBitmap(getCurrentBoard(), 0, 0, null);
}
ourHolder.unlockCanvasAndPost(canvas);
}
}
This is the Main Activity add button code
public void addButtons(int numOfButton) {
// create patameter
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
// create new button
Button newbutton = new Button(this);
// set background color
newbutton.setBackgroundColor(Color.BLUE);
// set width and height
newbutton.setWidth(30);
newbutton.setHeight(20);
// set position
newbutton.setY((float)numOfButton*20);
newbutton.setX(0);
// set text
newbutton.setText("new button");
// add button to the layout
buttons.addView(newbutton,p);// **the application crash here**
}
Due to the way android handles the Activities, the constructor onCreate is not guaranteed to have been called every time the addButtons method is executed
(See the Activity Lifecycle Section at developer.android.com)
You had better find the buttons LinearLayout
LinearLayout buttons = (LinearLayout) findViewById(R.id.LayoutButtons);
in addButtons and add the new button.
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.
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.