I have a MainActivity.java and NavigationDrawer. I want to when I click on items in navdrawer, start new layout. I know I can start new Activity, but I don't want a new activity. new layout is suitable for me. is this possible???
this is my ActivityMain.java:
public class MainActivity extends AppCompatActivity {
public Toolbar toolbar;
protected DrawerLayout drawerLayout;
NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
toolbar = (Toolbar) findViewById(R.id.toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.a1);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_more_vert_black_24dp);
navigationView = (NavigationView) findViewById(R.id.a2);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
item.setChecked(true);
drawerLayout.closeDrawer(Gravity.RIGHT);
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.khat) {
if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
drawerLayout.closeDrawer(Gravity.RIGHT);
}
else {
drawerLayout.openDrawer(Gravity.RIGHT);
}
return super.onOptionsItemSelected(item);
}
else {
Toast.makeText(getApplicationContext(),"SOMETHING",Toast.LENGTH_LONG).show();
}
return true;
}
public void onBackPressed(){
if (drawerLayout.isDrawerOpen(Gravity.RIGHT)){
drawerLayout.closeDrawer(Gravity.RIGHT);
}else {super.onBackPressed();}
}
}
and this is my second layout that I want start when I click on items:`
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#132740"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:textColor="#FFFFFF"
android:id="#+id/textView"
android:textSize="22sp"
android:layout_marginTop="20dp"
android:text="blah blah"/>
</RelativeLayout>
is this possible?
If is just the layout you wanna to change, there is a very simple soluction:
if (Case_A)
setContentView(R.layout.layout1);
else if (Case_B)
setContentView(R.layout.layout2);
But considering using Fragments:
https://developer.android.com/guide/components/fragments.html
With fragments, you will be able to divide your code in multiples class, for each layout. It will be more readable and light:
Here is an example
Use fragment in order to user multiple layout with different operation. If u just want to change the layout then u can set new layout on item click with setContentView(new layout); .
Related
I'm trying to change the background color of my layout to make dark/light theme on button click. But when I click on my button, my app crash and I don't know why.
Layout XML code :
<android.support.constraint.ConstraintLayout 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:id="#+id/layoutParam"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/bleuNuit"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="mcamus.ihm_smartphone.Parametres"
tools:showIn="#layout/app_bar_parametres">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginTop="24dp"
android:text="Thème de l'application"
android:textColor="#color/texteBlanc"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/thmClair"
android:onClick="thmClair"
android:layout_width="64dp"
android:layout_height="32dp"
android:layout_marginStart="48dp"
android:layout_marginTop="18dp"
android:text="Clair"
android:textSize="20px"
app:layout_constraintStart_toEndOf="#+id/textView"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/thmSombre"
android:onClick="thmSombre"
android:layout_width="64dp"
android:layout_height="32dp"
android:layout_marginStart="120dp"
android:layout_marginTop="18dp"
android:text="Sombre"
android:textSize="20px"
app:layout_constraintStart_toEndOf="#+id/textView"
app:layout_constraintTop_toTopOf="parent" />
Java code :
public class Parametres extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
LinearLayout paramLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parametres);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = 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.parametres, 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) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.alimentation) {
Intent alimentation = new Intent(Parametres.this,Accueil.class);
startActivity(alimentation);
} else if (id == R.id.capteurs) {
Intent capteurLayout = new Intent(Parametres.this,Capteurs.class);
startActivity(capteurLayout);
} else if (id == R.id.absence) {
Intent absLayout = new Intent(Parametres.this,Absence.class);
startActivity(absLayout);
} else if (id == R.id.parametres) {
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void thmSombre(View view) {
paramLayout = findViewById(R.id.layoutParam);
paramLayout.setBackgroundColor((Color.parseColor("#FFFFFF")));
}
public void thmClair(View view) {
}
}
I tried different method, but each time my application crashes. Someone would have an idea of the problem? I have to do this on homework for school and I do not know how to solve this problem.
You are trying to cast LinearLayout into a ConstraintLayout
Change line LinearLayout paramLayout; to ConstraintLayout paramLayout; in your activity
I have a MainActivity with navigation drawer. I created a fragment for each item in my nav drawer. I have a toolbar.xml that included in my content.xml and I want to set my content.xml as default layout for my fragments. but I want to make it scrollable because some of my fragments have many text & images!
how can I do that?
this is my MainActivity.java:
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private Toolbar toolbar;
NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.a1);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_more_vert_24dp);
navigationView = (NavigationView) findViewById(R.id.a2);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
item.setChecked(true);
drawerLayout.closeDrawer(Gravity.RIGHT);
int id = item.getItemId();
if (id == R.id.intro) {
IntroFragment introFragment = new IntroFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.layout_for_fragment, introFragment).commit();
}
return false;
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.khat) {
if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
drawerLayout.closeDrawer(Gravity.RIGHT);
}
else {
drawerLayout.openDrawer(Gravity.RIGHT);
}
return super.onOptionsItemSelected(item);
}
else {
Toast.makeText(getApplicationContext(), "Something", Toast.LENGTH_LONG).show();
}
return true;
}
public void onBackPressed(){
if (drawerLayout.isDrawerOpen(Gravity.RIGHT)){
drawerLayout.closeDrawer(Gravity.RIGHT);
}else {super.onBackPressed();}
}
}
this is my content.xml that I want to set it as my default layout for fragments.
<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:id="#+id/layout_for_fragment"
android:layout_height="match_parent"
android:background="#132740">
<include layout="#layout/toolbar" />
</RelativeLayout>
and this is my introFragment.xml:
<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:background="#color/colorPrimary"
tools:context="com.example.arsh.enc.IntroFragment">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="#+id/introfragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:lineSpacingExtra="10sp"
android:text="VERY VERY
VERY VERY
VERY VERY
VERY VERY
VERY VERY LONG TEXT"
android:textColor="#FFFFFF" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
I don't know how to do it.
can I do that? if yes, how?
Actually, you don't need to use ScrollView in your layout.
You can use
android:scrollbars = "vertical"
in your xml file.
And then in your MainActivity.java get your textView5 and try this:
textView5.setMovementMethod(new ScrollingMovementMethod());
Let me know, if that worked.
The following code is of BaseActivity and this activity is used to implement navigation drawer for different activity
public class BaseActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener {
private NavigationView navigationView;
private DrawerLayout fullLayout;
private Toolbar toolbar;
private ActionBarDrawerToggle drawerToggle;
private int selectedNavItemId;
#Override
public void setContentView(#LayoutRes int layoutResID) {
fullLayout = (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_base, null);
FrameLayout activityContainer = (FrameLayout) fullLayout.findViewById(R.id.activity_content);
getLayoutInflater().inflate(layoutResID, activityContainer, true);
/**
* Note that we don't pass the child's layoutId to the parent,
* instead we pass it our inflated layout.
*/
super.setContentView(fullLayout);
toolbar = (Toolbar) findViewById(R.id.toolbar);
navigationView = (NavigationView) findViewById(R.id.navigationView);
if (useToolbar())
{
setSupportActionBar(toolbar);
}
else
{
toolbar.setVisibility(View.GONE);
}
setUpNavView();
}
protected boolean useToolbar()
{
return true;
}
protected void setUpNavView()
{
navigationView.setNavigationItemSelectedListener(this);
if( useDrawerToggle()) { // use the hamburger menu
drawerToggle = new ActionBarDrawerToggle(this, fullLayout, toolbar,
R.string.nav_drawer_opened,
R.string.nav_drawer_closed);
fullLayout.setDrawerListener(drawerToggle);
drawerToggle.syncState();
} else if(useToolbar() && getSupportActionBar() != null) {
// Use home/back button instead
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(getResources()
.getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha));
}
}
protected boolean useDrawerToggle()
{
return true;
}
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
fullLayout.closeDrawer(GravityCompat.START);
selectedNavItemId = menuItem.getItemId();
return onOptionsItemSelected(menuItem);
}
#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
switch (id)
{
case R.id.action_main:
startActivity(new Intent(this, MainActivity.class));
return true;
case R.id.action_order:
startActivity(new Intent(this, Orders.class));
return true;
case R.id.rate_card:
startActivity(new Intent(this,RateCard.class));
return true;
}
return super.onOptionsItemSelected(item);
}
And here is xml file design which is used for implementation purpose
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="#+id/activity_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="?attr/colorPrimary"
app:theme="#style/MainActionBar"/>
<FrameLayout
android:id="#+id/activity_content"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigationView"
android:layout_width="wrap_content"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/menu_base"/>
</android.support.v4.widget.DrawerLayout>
I tried all thing but not getting proper way to implement.Any suggestion how to correct this will be helpful
I added a navigation drawer to my toolbar and added the toolbar to my HomePage Activity. The Drawer seems to be working fine but only pulls out when I slide my finger below the hamburger icon. Otherwise, if I simply click on the icon nothing happens. Does anybody know why? I watched the following tutorial and ideally when the hamburger icon is clicked, the drawer should come out: https://www.youtube.com/watch?v=Lfc1q1dFB3E
Is there an onClick attribute that I'm missing? Here is my code:
public class HomePage extends AppCompatActivity implements View.OnClickListener {
private DrawerLayout drawerLayout;
private ListView drawerList;
private ActionBarDrawerToggle drawerToggle;
String[] drawerListItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
Toolbar myToolbar = (Toolbar) findViewById(R.id.toolBarHome);
drawerLayout = (DrawerLayout)findViewById(R.id.homePageDrawer);
drawerList = (ListView)findViewById(R.id.homePageList);
drawerListItems = getResources().getStringArray(R.array.activities);
drawerList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,drawerListItems));
drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0: {
Intent i = new Intent(HomePage.this, HomePage.class);
startActivity(i);
break;
}
case 1: {
Intent i = new Intent(HomePage.this, Allergy.class);
startActivity(i);
break;
}
}
drawerLayout.closeDrawer(drawerList);
}
});
drawerToggle = new ActionBarDrawerToggle(this,drawerLayout,myToolbar,R.string.drawer_open,R.string.drawer_close)
{
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
syncState();
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
invalidateOptionsMenu();
syncState();
}
};
drawerLayout.setDrawerListener(drawerToggle);
setSupportActionBar(myToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
drawerToggle.syncState();
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.home:{
if (drawerLayout.isDrawerOpen(drawerList)){
drawerLayout.closeDrawer(drawerList);
}else{
drawerLayout.openDrawer(drawerList);
}
return true;
}
default:return super.onOptionsItemSelected(item);
}
}
Here is the XML layout. I have one ImageView on my Homepage Activity. When the NavigationDrawer pulls out, I need it to cover the existing ImageView, so I placed it within a relative layout within my drawerlayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#drawable/boardlayered"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolBarHome"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary">
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.DrawerLayout
android:id="#+id/homePageDrawer"
android:layout_below="#+id/toolBarHome"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/dogPic"
android:background="#drawable/happy_dog"
android:layout_width="175dp"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:layout_marginLeft="20dp" />
</RelativeLayout>
<ListView
android:id="#+id/homePageList"
android:background="#android:color/white"
android:layout_gravity="start"
android:layout_width="305dp"
android:layout_height="match_parent"></ListView>
</android.support.v4.widget.DrawerLayout>
Found it. The issue is in this piece of code, which is directly responsible for the hamburger icon to perform an action:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.home:{
if (drawerLayout.isDrawerOpen(drawerList)){
drawerLayout.closeDrawer(drawerList);
}else{
drawerLayout.openDrawer(drawerList);
}
return true;
}
default:return super.onOptionsItemSelected(item);
}}
Specifically
case R.id.home:
Should be
case android.R.id.home:
This latter case syntax is what works
I think the problem here is with the lines:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
It seems this will try to use your toggle button as an 'up' navigation button instead, if you're trying to use the hamburger button to open and close your drawer you can remove these lines.
Suppose I have two buttons, one is an action button which is in the action bar(#+id/button1). And another is common button in the layout(#+id/button2).
How can I set the button1 disabled when i click the button2?
findViewById(button1) seems not work. it will return null.
this is my menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/button1"
android:title="submit"
android:showAsAction="always" />
</menu>
and this is my mainacticity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.button1 ) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
button1 = (Button) findViewById(R.id.button1);/*which return null*/
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
button1.setEnabled(false);/*what i failed to do*/
}
});
}
1.Create a variable MenuItem in your Activity class
MenuItem menuItem;
2. Find your variable in onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
menuItem = menu.findItem(R.id.item_circuit);
return super.onCreateOptionsMenu(menu);
}
Disable the item in your buttonClick method
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
menuItem.setEnabled(false);
}
});
Try this
public void button1click(View v)
{
Button button2=(Button)getActionBar().getCustomView().findViewById(R.id.button2);
button2.setClickable(false);
button2.setEnabled(false);
}
You have to inflate the layout/views before using findViewById.
Like that:
private Toolbar toolbar;
// Set a Toolbar to replace the ActionBar.
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
cartLayout = (RelativeLayout) toolbar.findViewById(R.id.rl_cart);
Your toolbar layout would be like this:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right">
<RelativeLayout
android:id="#+id/rl_cart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:padding="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:src="#drawable/ic_cart" />
</RelativeLayout>
</RelativeLayout>
You can find more answer here:
How can I implement custom Action Bar with custom buttons in Android?
How to set two custom actionbar button in left and right in android?