Parent Layout visiblity Invisible/visible on Button Click - android

I am trying to change visibility of Relative Layout in my code on Button click, But i am facing a weird behavior.
Whenever i click on my Button say A first time, it shows the Relative layout say RA, and after clicking it(Second time) again it hides RA. The problem i am facing here is when i click on A second time it also hides its parent layout, And then on clicking it again it shows RA and its parent layout, After that it hides and shows both layouts on click event.
How this is happening here,
Here is my Code
this is my XML
<RelativeLayout
android:id="#+id/mainscreenrelativeLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_margin="3dp"
android:background="#ffffff" >
<Button
android:id="#+id/imagebuttonSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:background="#drawable/setting_icon_hover"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/RL_mainpage_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/mainscreenrelativeLayout2"
android:layout_alignParentRight="true"
android:visibility="invisible"
>
<Button
android:id="#+id/button_settings_monthlyissue"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/monthly_issue_select"/>
<Button
android:id="#+id/button_settings_changepassword"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button_settings_monthlyissue"
android:background="#drawable/password_select" />
<Button
android:id="#+id/button_settings_logout"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button_settings_changepassword"
android:background="#drawable/logout_select" />
</RelativeLayout>
And this is my CODE in Activity Class:
Button _settings;
RelativeLayout __settingslayout;
boolean isSettingsClicked = false;
........
........
_settings = (Button) findViewById(R.id.imagebuttonSettings);
_settingslayout = (RelativeLayout) findViewById(R.id.RL_mainpage_settings);
_settings.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (isSettingsClicked == false) {
_settings.setBackgroundResource(R.drawable.setting_icon);
_settingslayout.setVisibility(View.VISIBLE);
isSettingsClicked = true;
} else if (isSettingsClicked == true) {
_settingslayout.setVisibility(View.INVISIBLE);
_settings
.setBackgroundResource(R.drawable.setting_icon_hover);
isSettingsClicked = false;
}
}
});
The problem is , RelativeLayout with id android:id="#+id/mainscreenrelativeLayout2" is also hiding on click event, as you can see i haven't used this id in my Activity.

Related

Android: Floating Actions Menu on click not working

I have an Activity (HomeActivity) with three tabs. Each tab has it own fragment and in one fragment I have Floating Actions Menu with 3 Floating Action Buttons. Here's the XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_home_layout"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Recent"
android:textAppearance="?android:attr/textAppearanceLarge" />
<com.getbase.floatingactionbutton.FloatingActionsMenu
android:id="#+id/fabMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_margin="16dp"
android:onClick="fabMenuClicked"
app:fab_addButtonColorNormal="?attr/colorAccent"
app:fab_addButtonColorPressed="#00b1c7"
app:fab_labelStyle="#style/fab_menu_label_style">
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="#+id/singleChatFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fab_colorNormal="?attr/colorAccent"
app:fab_colorPressed="#00b1c7"
app:fab_size="mini"
app:fab_title="New chat"/>
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="#+id/groupChatFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fab_colorNormal="?attr/colorAccent"
app:fab_colorPressed="#00b1c7"
app:fab_size="mini"
app:fab_title="New group chat"/>
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="#+id/serviceChatFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fab_colorNormal="?attr/colorAccent"
app:fab_colorPressed="#00b1c7"
app:fab_size="mini"
app:fab_title="Request a service"/>
</com.getbase.floatingactionbutton.FloatingActionsMenu>
</RelativeLayout>
It expands on click and it works fine. The thing I want to do is when Floating Action Menu is clicked, change opacity of a layout that holds it (Relative Layout with id: main_home_layout).
But my on click method is never entered. I tried placing it in Activity that holds the fragment, it didn't work. I tried placing in the fragment, still nothing. I ran app in debug mode, break point is never catched.
This is on click method:
public void fabMenuClicked(View v) {
final FloatingActionsMenu floatingActionsMenu = (FloatingActionsMenu) v;
findViewById(R.id.main_home_layout).getBackground().setAlpha(128);
}
Does anyone has an idea why it won't work. Thanks.
Try using setOnFloatingActionsMenuUpdateListener, change the color as per your need in overriden onMenuExpanded and onMenuCollapsed method, like :
FloatingActionsMenu floatingMenu = v.findViewById(R.id.fabMenu);
((FloatingActionsMenu)floatingMenu).setOnFloatingActionsMenuUpdateListener(new FloatingActionsMenu.OnFloatingActionsMenuUpdateListener() {
#Override
public void onMenuExpanded() {
findViewById(R.id.main_home_layout).getBackground().setAlpha(128); //change opacity here
}
#Override
public void onMenuCollapsed() {
findViewById(R.id.main_home_layout).getBackground().setAlpha(64); //change opacity here
}
});

Problems with setvisibility for button on RelativeLayout Eclipse

Hy.... I'm making a simple project on eclipse. I'm using relative layout as shown below :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".ProfileActivity"
>
<Button
android:id="#+id/Prof_edit_btn"
android:layout_below="#+id/Prof_LL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit"
android:layout_marginTop="17dp"
android:layout_centerHorizontal="true"
/>
<LinearLayout
android:layout_below="#+id/Prof_LL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
>
<Button
android:id="#+id/Prof_save_btn"
android:layout_below="#+id/Prof_LL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:layout_marginTop="17dp"
android:layout_centerHorizontal="true"
android:visibility="invisible"
android:clickable="false"
/>
<Button
android:id="#+id/Prof_batal_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Batal"
android:layout_marginTop="17dp"
android:visibility="invisible"
android:clickable="false"
/>
</LinearLayout>
</RelativeLayout>
In the main activity, I've made a simple onClicklistener for Edit and Save button as shown below :
mEdit = (Button)findViewById(R.id.Prof_edit_btn);
mEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mBatal.setVisibility(View.VISIBLE);
mBatal.setClickable(true);
mSave.setVisibility(View.VISIBLE);
mSave.setClickable(true);
mEdit.setVisibility(View.INVISIBLE);
}
});
mSave = (Button)findViewById(R.id.Prof_save_btn);
mSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mEdit.setVisibility(View.VISIBLE);
mBatal.setVisibility(View.INVISIBLE);
mSave.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(), "Data Anda berhasil disimpan", Toast.LENGTH_SHORT).show();
}
});
Well... As we can see from my layout xml, only EDIT button will be visible on the start, then SAVE and BATAL button will be visible when EDIT is clicked by user according to the code program within setonclick listener of EDIT button. And as soon as EDIT button is clicked it will be invisible.... The problems here are WHen I clicked EDIT button, only SAVE button will be visible and BATAL button will be visible when SAVE button is clicked, another problem that I'm facing now is EDIT button is not invisible at all when I click it.... So.. anyone can help me with this problem please...??? :'(
Thanks in advance....
Naaah... I found my own solution... Just set the visibility of SAVE btn and BATAL btn to GONE... And it's all done.... :-)
<Button
android:id="#+id/Prof_save_btn"
android:layout_below="#+id/Prof_LL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:layout_marginTop="17dp"
android:layout_centerHorizontal="true"
android:visibility="gone"<-----------=
android:clickable="false"
/>
<Button
android:id="#+id/Prof_batal_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Batal"
android:layout_marginTop="17dp"
android:visibility="gone"<-----------=
android:clickable="false"
/>

Characters appear in the order as i click

For example. There is picture on the top of the screen below that there are some empty boxes and below the boxes there are some buttons. Every button has a character for text("a","c","t"). You click on a button and the button's text appear in the box. You can click them in the order you want to but the answer is "cat" so when you put the characters in the correct order then you got a toast.
I tried to do it with TextViews and Buttons. I can make the button disappear when i click on it and a textview appear in the same time. But every textview has a fix place on the screen, so i need to put every character in every box invisible and when i click on the "c" character it appear in the first box and the other "c" characters stay invisible. But if i click on the "a" first, then it appears in the second box because there is too much variation to do all. I'm not good at explaining but if anyone has an idea how to do that easier please response!
Here is my code:
public class MainActivity extends ActionBarActivity implements OnClickListener{
Button b1;
Button b2;
Button b3;
TextView tg1;
TextView tg2;
TextView tg3;
TextView to1;
TextView to2;
TextView to3;
TextView tl1;
TextView tl2;
TextView tl3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.bg);
b1.setOnClickListener(this);
b2 = (Button)findViewById(R.id.bo);
b2.setOnClickListener(this);
b3 = (Button)findViewById(R.id.bl);
b3.setOnClickListener(this);
tg1 = (TextView)findViewById(R.id.tg1);
tg2 = (TextView)findViewById(R.id.tg2);
tg3 = (TextView)findViewById(R.id.tg3);
to1 = (TextView)findViewById(R.id.to1);
to2 = (TextView)findViewById(R.id.to2);
to3 = (TextView)findViewById(R.id.to3);
tl1 = (TextView)findViewById(R.id.tl1);
tl2 = (TextView)findViewById(R.id.tl2);
tl3 = (TextView)findViewById(R.id.tl3);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.bg:
b1.setVisibility(View.INVISIBLE);
tg1.setVisibility(View.VISIBLE);
tg2.setVisibility(View.INVISIBLE);
tg3.setVisibility(View.INVISIBLE);
break;
case R.id.bo:
b2.setVisibility(View.INVISIBLE);
to2.setVisibility(View.VISIBLE);
to1.setVisibility(View.INVISIBLE);
to3.setVisibility(View.INVISIBLE);
break;
case R.id.bl:
b3.setVisibility(View.INVISIBLE);
tl3.setVisibility(View.VISIBLE);
tl2.setVisibility(View.INVISIBLE);
tl1.setVisibility(View.INVISIBLE);
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="hu.szada.gombokelso.MainActivity"
android:orientation="horizontal">
<TextView
android:id="#+id/tl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="invisible"
android:text="l"/>
<Button
android:id="#+id/bo"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="86dp"
android:onClick="onClick"
android:text="o" />
<Button
android:id="#+id/bl"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/bg"
android:layout_alignBottom="#+id/bg"
android:layout_alignParentLeft="true"
android:layout_marginLeft="36dp"
android:onClick="onClick"
android:text="l" />
<Button
android:id="#+id/bg"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginRight="14dp"
android:layout_toLeftOf="#+id/bo"
android:onClick="onClick"
android:text="g" />
<TextView
android:id="#+id/tg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tl"
android:layout_alignBottom="#+id/tl"
android:layout_alignLeft="#+id/tl"
android:layout_weight="1"
android:visibility="invisible"
android:text="g" />
<TextView
android:id="#+id/to1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tl"
android:layout_alignBottom="#+id/tl"
android:layout_alignLeft="#+id/tl"
android:layout_weight="1"
android:visibility="invisible"
android:text="o" />
/// Second
<TextView
android:id="#+id/to2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tl1"
android:layout_alignBottom="#+id/tl1"
android:layout_marginLeft="19dp"
android:layout_toRightOf="#+id/tl1"
android:layout_weight="1"
android:visibility="invisible"
android:text="o" />
<TextView
android:id="#+id/tg2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/to2"
android:layout_alignBottom="#+id/to2"
android:layout_alignLeft="#+id/to2"
android:layout_weight="1"
android:visibility="invisible"
android:text="g" />
<TextView
android:id="#+id/tl2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/to2"
android:layout_alignBottom="#+id/to2"
android:layout_alignRight="#+id/to2"
android:layout_weight="1"
android:visibility="invisible"
android:text="l" />
/// Third
<TextView
android:id="#+id/tg3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/to2"
android:layout_alignBottom="#+id/to2"
android:layout_alignRight="#+id/bl"
android:layout_weight="1"
android:visibility="invisible"
android:text="g" />
<TextView
android:id="#+id/tl3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tg3"
android:layout_alignBottom="#+id/tg3"
android:layout_alignLeft="#+id/tg3"
android:layout_weight="1"
android:visibility="invisible"
android:text="l" />
<TextView
android:id="#+id/to3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tg3"
android:layout_alignBottom="#+id/tg3"
android:layout_toRightOf="#+id/tl3"
android:layout_weight="1"
android:visibility="invisible"
android:text="o" />
You might want to try a slightly different approach.
If I understand you correctly, you want to "type" a word out using given lettered buttons. Like one of those hangman style games.
Why not append the text views on the fly.
Something like
#Override
public void onClick(View v) {
//Grab the surrounding layout for the textviews
GridView answerGrid = (GridView)getViewById(R.id.answerGrid);
//Get the text that was on the button
Button b = (Button)v;
String btnText = b.getText().toString();
//Make a text view with text
TextView txt = new TextView();
text.setText(btnText);
//Append to text view container
answerGrid.addView(txt);
//Invisible button
b.setVisibility(View.INVISIBLE);
}
Haven't tested to see if this is perfect, but its a start.
=====
I've looked at your xml
Why not use GridViews?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
....>
<GridView android:id="#+id/answerGrid"
....>
<!-- Put nothing here. This is for answers -->
</GridView>
<GridView android:id="#+id/lettersGrid"
android:layout_below="answerGrid"
....>
<!-- Buttons in here -->
</GridView>
</RelativeLayout>
This way you can customise the number of rows/columns based on the length of the word you're playing with. And GridView will automatically give you a neat layout and spacing.
Have a look at the GridView doc and get it customised the way you want it.
See my edits above for the Java code.

Use buttons in game-style scenario

I have a 5 linear layouts, each containing 10 buttons, which gives rise to a 5 by 10 array of buttons. I would like the user to select 5 buttons, and each button contains a certain point value. On the next page, I would like the sum of the point values of these 5 buttons to appear in a textview.
Here is what I have tried so far, using a small sample of my code.
On the xml file: (this is a 2 by 3 sample)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button11"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="50" />
<Button
android:id="#+id/button12"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="50" />
<Button
android:id="#+id/button13"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="75" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button21"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="00" />
<Button
android:id="#+id/button22"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="25" />
<Button
android:id="#+id/button23"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="75" />
</LinearLayout>
I am not sure what to do on the java file, but I was considering giving each button id a value (which is currently represented by the name of the button) and adding up all the values, which will then be displayed on the next page.
You can just convert the text on the button to an integer:
int value = 0;
try {
value = Integer.parseInt(button.getText().toString());
}
catch(NumberFormatException nfe) {
}
Say that xml file is called activity_main.xml. You need to have an Activity class, let's call it MainActivity.java
MainActivity.java
public class MainActivity extends Activity implements OnClickListener{
private totalVal = 0;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button12 = (Button) findViewById(R.id.button12);
button12.setOnClickListener(this);
// ... Do the same for the rest of the buttons
}
#Override
public void onClick(View v){
switch(v.getId(){
case R.id.button12:
int textVal = Integer.parseInt(v.getText().toString());
totalVal = totalVal + textVal;
// do whatever else you want to when the button is clicked
break;
// ... Do the same for the rest of the buttons
}
You would also need a button that says you are done, and implement it in a similar way

how to use viewswitcher with same id's in both layouts

I am using view switcher in my project.
In my xml I have created 2 layouts with all same Id's.
After I switch my view I can not switch to previous view because I am using same ID's in both layouts.
Now how can I use one listener in java code for both layouts in view switcher.
I dont want to create an another id and create another listener to switch again.
My xml is as below.
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/profileSwitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="#string/hello_world" />
<Button
android:id="#+id/switchBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Switch" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="#string/hello_world" />
<Button
android:id="#+id/switchBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Switch" />
</RelativeLayout>
</ViewSwitcher>
My java code is as follows
final ViewSwitcher switcher = (ViewSwitcher) findViewById(R.id.profileSwitcher);
Button btn = (Button) findViewById(R.id.switchBtn);
btn.setOnClickListener(new OnClickListener() {
private boolean switchCheck;
public void onClick(View v) {
new AnimationUtils();
switcher.setAnimation(AnimationUtils.makeInAnimation(getApplicationContext(), true));
if (!switchCheck) {
switcher.showNext();
switchCheck = true;
} else {
switcher.showPrevious();
switchCheck = false;
}
}
});
Please help..
Crete different ids for parent relative layout in xml.
And then in java file check which relative layout button is used.
using method
step 1)
RelativeLayout r1 = RelativeLayout findviewbyId(R.id.rl1)
RelativeLayout r2 = RelativeLayout findviewbyId(R.id.rl2)
step2)
if button.getParent equals to R1 then ...
else .....
Just try to change ur text in any of the button or textView .may be ur view is switching but because the code for both the layouts is exactly same u r not able to differentiate.

Categories

Resources