Reputation: 51
I want to add two linear layouts at same position with width same as parent. When I press Button1 linearLayout1 should appear and on Button2 LinearLayout2 should appear. How to do this in android?
Upvotes: 1
Views: 974
Reputation: 10829
You can combine multiple fragments in a single activity and reuse a fragment in multiple activities too. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running.
Here's how it can be done using Fragment:
First create a layout file with 2 buttons and a Framelayout, we will write code later to replace the framelayout with the contents in the fragment.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
<LinearLayout
android:layout_width="match_parent"
android:id="@+id/ll1"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1"
android:id="@+id/bt1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2"
android:id="@+id/bt2"
android:layout_alignBottom="@+id/frame"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</LinearLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ll1"
android:id="@+id/frame"/>
</RelativeLayout>
Next create 2 fragments, for eg FragmentA and fragmentB
public class fragmentA extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.l1, container, false); //create a layout here
// add some code to set some text for eg
return v;
}
}
Create a second fragment B
public class fragmentB extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.l1, container, false); //create a layout here
// add some code to set some text for eg
return v;
}
}
Now add the Activity code with 2 buttons, when the button 1 is clicked invoke fragmentManager.beginTransaction().replace(R.id.frame, new fragmentA()).commit();
, this will replace the framelayout with the contents of the fragmentA, similaryly when button 2 is clicked invoke fragmentManager.beginTransaction().replace(R.id.frame, new fragmentB()).commit();
Complete code:
public class MyFragment extends ActionBarActivity implements View.OnClickListener {
Button bt1,bt2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tt);
bt1=(Button)findViewById(R.id.bt1);
bt2=(Button)findViewById(R.id.bt2);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getSupportFragmentManager();
switch (v.getId()) {
case R.id.bt1:
fragmentManager.beginTransaction()
.replace(R.id.frame, new BlockCallers()).commit();
break;
case R.id.bt2:
fragmentManager.beginTransaction()
.replace(R.id.frame, new smsSetting()).commit();
}
}
}
For tutorials refer to this link(official link)
Upvotes: 0
Reputation: 2509
Try this: First create your_xml_file.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/firstLinear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
</LinearLayout>
<LinearLayout
android:id="@+id/secondLinear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:focusable="false"
android:layout_centerInParent="true"
android:background="@drawable/ic_launcher"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:focusable="false"
android:background="@drawable/ic_launcher"
android:layout_below="@id/button1"
android:layout_centerHorizontal="true" />
</RelativeLayout>
And java clas...
public class YourActivity extends Activity {
private Button button1,button2;
private LinearLayout linearLayout1,linearLayout2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourXml);
button1 = (Button).findViewById(android.R.id.button1);
button2 = (Button).findViewById(android.R.id.button2);
linearLayout1 = (LinearLayout).findViewById(android.R.id.firstLinear);
linearLayout2 = (LinearLayout).findViewById(android.R.id.secondLinear);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearLayout1.setVisibility(View.VISIBLE);
linearLayout2.setVisibility(View.GONE);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearLayout2.setVisibility(View.VISIBLE);
linearLayout1.setVisibility(View.GONE);
}
});
}
Hope this will help you.Happy coding :)
Upvotes: 2
Reputation: 1820
If you are not using Fragment
for task, you can simply use setVisibility(View.VISIBLE)
and setVisibility(View.INVISIBLE)
/ setVisibility(View.GONE)
( if other UI elements don't depend on view you'll set GONE)
on onClick
of buttons. It'll look like this:
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearLayout1.setVisibility(View.VISIBLE);
linearLayout2.setVisibility(View.INVISIBLE);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearLayout1.setVisibility(View.VISIBLE);
linearLayout2.setVisibility(View.INVISIBLE);
}
});
Upvotes: 0
Reputation: 59
You can use the method setVisibility() in your code, as:
view.setVisibility(View.GONE);
view.setVisibility(View.VISIBLE);
Upvotes: 0