Sunny Sunny - 4 months ago 22
Android Question

Hide all Relative Layout with a Function except one we choose

I have created 5 Relative Layouts in Android Studio Project (This will be more that 40) with same size. I also create Buttons so that if Someone Clicks on Lesson 1 button than Only layout1(id of first Layout) Layout Shows on the Screen and Other Layout automatically Hide, if user clicks on Lesson 2 Button than layout2(id of second layout) show and rest hides automatically, and so on...

Can anyone tell the Logic that I can use to do this. And if there is another best way to do this pls tell....
This is my Lessons.java file

public class Lessons extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

//Relative Layout's Variables Declaration here
RelativeLayout r[];

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lessons);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);

}

@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.lessons, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Toast.makeText(Lessons.this, "Settings", Toast.LENGTH_SHORT).show();
return true;
}

return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();


switch (id){
case R.id.lesson_1:
hideRelativeLayout(R.id.layout1);
break;
case R.id.lesson_2:
hideRelativeLayout(R.id.layout2);
break;
/*Buttons onSelect Event upto 40*/

}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
protected void hideRelativeLayout(int current){
/*Code that Shows only current Layout and hide rest of all layouts*/

}
}


and this is my activity_lessons.xml file

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:id="@+id/relLay"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.kaliattacks.evilandroid.hackinginhindi.Lessons"
tools:showIn="@layout/app_bar_lessons">


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:id="@+id/layout1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<!--This is Layout 1-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Layer 1"
android:id="@+id/textView1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<!--This is Second Layout-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout2"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is another layer"
android:id="@+id/textView2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

</RelativeLayout>
<!--This is Third Layout-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout3"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Third Layout Text"
android:id="@+id/textView3"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

</RelativeLayout>
<!--This is Fourth Layout-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout4"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Fourth Layout Text"
android:id="@+id/textView4"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

</RelativeLayout>
<!--Fifth Layout-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout5"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Fifth Layout Text"
android:id="@+id/textView5"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

</RelativeLayout>
</RelativeLayout>

Answer

You can put all RelativeLayout id in an Integer ArrayList like

ArrayList<Integer> relativeLayoutIdList = new ArrayList<Integer>();

then in onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    // add all relativelayout id to arraylist
    relativeLayoutIdList.add(R.id.layout1);
    relativeLayoutIdList.add(R.id.layout2);
    ...

}

and in your hideRelativeLayout

protected void hideRelativeLayout(int current){
   for (int i = 0, len = relativeLayoutIdList.size(); i < len; i++){
       if(relativeLayoutIdList.get(i) == current){
            // visible current relativelayout
            (RelativeLayout)  findViewById(relativeLayoutIdList.get(i)).setVisibility(View.VISIBLE);
       }else{
            // hide all rest relativelayout
            (RelativeLayout)  findViewById(relativeLayoutIdList.get(i)).setVisibility(View.GONE);
       }
   }
}

Suggest: I see you use NavigationView, you can use mutilple Fragment in NavigationView instead of create many RelativeLayout https://guides.codepath.com/android/Fragment-Navigation-Drawer

Comments