setting layouts visiblity on button click event - android

I am new to android and i am unable to solve my simple problem.I have a parent Tablelayout and inside it i have two tablelayouts with ids tbl1 and tbl2 respectively in my xml file.In tbl1 layout i have three textviews and three edittext controls similarly i have some views in tbl2 layout.Now i want that my tbl1 layout is visible when my activity starts but on click of my button2 which is in tbl1 layout my tablelayout tbl1 gets invisible and my tablelayout tbl2 becomes visible.Actually i know i can achieve this in asp.net with the help of panels but in android i am not able to achieve the same thing.Please help

You are going to want to look at the setVisibility() method. In the on click listeners for button 2, put the following;
Button.setVisibility(View.INVISIBLE)
TextView.setVisibility(View.INVISIBLE)
etc...
This will make the views invisible, but they will still take up space. If you don't want them to take up space you should use
setVisibility(Veiw.GONE);
Finally, to make you buttons and textview and edittexts in the second table, appear, you need to do the following;
setVisibility(View.VISIBLE);

Java Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.visibility_1);
// Find the view whose visibility will change
mVictim = findViewById(R.id.victim);
// Find our buttons
Button visibleButton = (Button) findViewById(R.id.vis);
Button invisibleButton = (Button) findViewById(R.id.invis);
Button goneButton = (Button) findViewById(R.id.gone);
// Wire each button to a click listener
visibleButton.setOnClickListener(mVisibleListener);
invisibleButton.setOnClickListener(mInvisibleListener);
goneButton.setOnClickListener(mGoneListener);
}
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
mVictim.setVisibility(View.VISIBLE);
}
};
OnClickListener mInvisibleListener = new OnClickListener() {
public void onClick(View v) {
mVictim.setVisibility(View.INVISIBLE);
}
};
OnClickListener mGoneListener = new OnClickListener() {
public void onClick(View v) {
mVictim.setVisibility(View.GONE);
}
};
}
XML Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:background="#drawable/box"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:background="#drawable/red"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/visibility_1_view_1"/>
<TextView android:id="#+id/victim"
android:background="#drawable/green"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/visibility_1_view_2"/>
<TextView
android:background="#drawable/blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/visibility_1_view_3"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:id="#+id/vis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/visibility_1_vis"/>
<Button android:id="#+id/invis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/visibility_1_invis"/>
<Button android:id="#+id/gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/visibility_1_gone"/>
</LinearLayout>
</LinearLayout>

Related

Removing Views dynamically

I search a lot for this, but I can't solve it. I have LinearLayout with two textviews and a button (to add others LinearLayout) like this:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<EditText android:id="#+id/ingredientsField"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="#dimen/boxes_margin"
android:hint="#string/ingredients"
android:inputType="text"
xmlns:android="http://schemas.android.com/apk/res/android" />
<EditText
android:id="#+id/quantityField"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="#dimen/boxes_margin"
android:hint="#string/quantity"
android:inputType="number"
/>
<com.google.android.material.button.MaterialButton
android:id="#+id/add_ingredient_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add"
android:layout_margin="#dimen/boxes_margin"
app:icon="#drawable/ic_add_ingr_btn"
/>
</LinearLayout>
The layout added is like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="50dp">
<EditText android:id="#+id/ingredientsField"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="#dimen/boxes_margin"
android:hint="#string/ingredients"
android:inputType="text"
xmlns:android="http://schemas.android.com/apk/res/android" />
<EditText android:id="#+id/quantityField"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="#dimen/boxes_margin"
android:hint="#string/quantity"
android:inputType="number"
xmlns:android="http://schemas.android.com/apk/res/android" />
<ImageButton
android:id="#+id/remove_ingredient_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="#dimen/boxes_margin"
android:src="#drawable/ic_remove_ing_qnt"/>
</LinearLayout>
In the Activity, I created a method to add new Layouts (and it works), and inside of it a method to delete the corresponding Layout with the Delete Button. Delete Button works only one time and only if it's in the first layout added. Here's the code:
add_ingredient.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.ingredient_quantity_layout, null);
// Add the new row before the add field button.
parentIngredientLayout.addView(rowView);
ImageButton removeChildIngredient = findViewById(R.id.remove_ingredient_btn);
removeChildIngredient.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
parentIngredientLayout.removeView((View) v.getParent());
}
});
}
});
You have to set the OnClickListenerevent on the ImageButton from the last inserted layout.
To do that, just change this line
ImageButton removeChildIngredient = findViewById(R.id.remove_ingredient_btn);
to
ImageButton removeChildIngredient = rowView.findViewById(R.id.remove_ingredient_btn);
rowView.findViewById search the ImageButton on the last inserted layout and not the whole layout which contains other layouts.
In this line :
parentIngredientLayout.removeView((View)
v.getParent());
You are trying to remove the parent of the removeChildIngredient ImageButton, which is probably parentIngredientLayout, which i think is not what you want,
You could try this :
parentIngredientLayout.
removeView(rowView);
But in your implementation, you could encounter issues when you will add multiples ingredients, because you are setting a new onClickListener to each new ingredient, and The ImageButton will only delete the last one you have added(the last onClickListener set),
Instead, you could use a List/RecyclerView or search for another implementation, eg. put the ImageButton inside the Inflated Layout so you'll have one Remove Button in each Layout you add,
Then you should replace the findViewById of your ImageButton by rowView.findViewById
And this line should stay unchanged
parentIngredientLayout.removeView((View) v.getParent());

Add a EditText with button

I want when I click add icon (+) as this image, it will add more a EditText to add another address. Number address can expand as user want. How I can do that ?
Add a EditText
First of all you need to take container in which you can addviews at runtime :
For example:
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"/>
<Button
android:id="#+id/addAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
Now you need to add edit text at runtime.
final LinearLayout container = (LinearLayout) findViewById(R.id.container);
Button button = (Button) findViewById(R.id.addAddress);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText editText = new EditText(HomeActivity.this);
container.addView(editText);
}
});
Please read more on how to add views at runtime.

In which control should I place my controls to be able to hide/show them?

I have an activity with the following layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button ....
<EditText ....
//..........
I want to place a group of controls into the 2nd LinearLayout so that I'll be able to hide them all at once when I click the button. I suppose I should wrap them in one more LinearLayout and hide/show it when I click the button.
I've tried that and got an error saying basically it wasn't allowed. Did I do anything wrong with that? If not, what should I use instead?
As you need to hide/show your layout containing some views, on button click:
LinearLayout ll = (LinearLayout)findViewById(R.id.your_layout_id);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
if(ll.getVisibility() == View.VISIBLE)
{
ll.setVisibility(View.INVISIBLE); //use View.GONE, if required
}
else
{
ll.setVisibility(View.VISIBLE);
}
}
}

Replace the content of Linear layout at onclick button android

I want to replace the content of the linear layout which has button and textview with an editText type="phone" after an onclick event of button. They're all in the located in the same page.
Is there a way to do that?
Use following code to remove all Views.
lauout.removeAllViews();
You'll have to make two separate fragments and use a fragmenttransaction to replace one with the other.
See fragmenttransactions:
http://developer.android.com/reference/android/app/FragmentTransaction.html
What I can understand from your question
1) There are 3 views Button, TextView and EditText under one LinearLayout
Like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My TextView " />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone" // with input type phone
android:visibility="gone" />
</LinearLayout>
And You want to remove the textview and replace it with Edit Text on Button Click
like this
tv=(TextView)findViewById(R.id.textView1);
et=(EditText)findViewById(R.id.editText1);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setVisibility(View.GONE);
et.setVisibility(View.VISIBLE);
}
});
place all of your three elements in the layout in your xml_layout, and set the EditText visibility to "gone", then when clicking the Button you mentioned, just set the Button and the TextView visibility to "gone" and the EditText visibility to "visible":
<LinearLayout android:id="#+id/layout"
.
.
.>
<Button android:id="#+id/button"
.
.
.
android:visibility="visible"/>
<TextView android:id="#+id/text"
.
.
.
android:visibility="visible"/>
<EditText android:id="#+id/edit_text"
.
.
.
android:inputType="phone"
android:visibility="gone"/>
</LinearLayout>
when clicking the Button:
public OnClickListener onButClick = new OnClickListener() {
#Override
public void onClick(View v) {
button.setVisibility(View.GONE);
text.setVisibility(View.GONE);
text_view.setVisibility(View.VISIBLE);
}
};

Button onClick not detected with a ListView and ListActivity

I have the following layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:text="Height"
android:layout_height="wrap_content"
android:id="#+id/buttonHeight"
android:layout_width="wrap_content"
android:layout_weight="15"
android:onClick="OnClickHeight">
</Button>
<Button
android:text="Width"
android:layout_height="wrap_content"
android:id="#+id/buttonWidth"
android:layout_width="wrap_content"
android:layout_toRightOf="#id/buttonHeight"
android:layout_weight="1"
android:onClick="onClickWidth">
</Button>
</LinearLayout>
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="1px"
android:drawSelectorOnTop="false"/>
</RelativeLayout>
I then have a class that extends ListActivity in which I have my onClickWidth and onClickHeight methods, for example:
public void onClickWidth(View v)
{
// does stuff
}
However, these onClick events are not being handled. If I change the class to extend Activity, instead of ListActivity, and also remove the ListView from my layout, then it is detected!
I have tried to set android:clickable="true", played around with the android:focusable attribute, and various other things, but I just cannot get this to work. How can I resolve this, or is this simply not allowed?
Can you tell us what are you try to do? And why do you need ListActivity?
Sorry for edditing:P
You could take a look here: How to handle ListView click in Android
put OnClicklistener within adapter itself .
Rather then using the onClick stuff in the layout, have you tried something like this:
Button buttonWidth = (Button)findViewById(R.id.buttonWidth);
buttonWidth.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//do stuff
}
});
Then do the same for buttonHeight.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_land);
Button btn = (Button) findViewbyid(R.id.buttonWidth);
btn.setOnClickListener(this);
}
public void onClickWidth(View v)
{
if(v == btn){
//your code...
}
}
set the android:descendantFocusability attribute of the parent layout which contains the ListView to value blocksDescendants like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"> ...</LinearLayout>

Categories

Resources