Nested LinearLayout doesn't work in Android - android

I am trying to make layout like in the picture. Project picture I can only link it because I am new. When I run the code with just one LinearLayout it works fine. But when I make nested linear layout it shows an only white screen. And there are no errors. The code adds a score to teams according to their throw s.Thank you.
Java code:
public class MainActivity extends AppCompatActivity {
int scorea=0;
int scoreb=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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);
}
public void displayforpointb3(View view){
TextView scoreView=(TextView)findViewById(R.id.team_b_score);
scoreb+=3;
scoreView.setText(""+scoreb);
}
public void displayforpointb2(View view){
TextView scoreView=(TextView)findViewById(R.id.team_b_score);
scoreb+=3;
scoreView.setText(""+scoreb);
}
public void displayforfreethrowb(View view){
TextView scoreView=(TextView)findViewById(R.id.team_b_score);
scoreb++;
scoreView.setText(""+scoreb);
}
public void displayforpoint3(View view){
TextView scoreView=(TextView)findViewById(R.id.team_a_score);
scorea+=3;
scoreView.setText(""+scorea);
}
public void displayforpoint2(View view){
TextView scoreView=(TextView)findViewById(R.id.team_a_score);
scorea+=2;
scoreView.setText(""+scorea);
}
public void displayforfreethrow(View view){
TextView scoreView=(TextView)findViewById(R.id.team_a_score);
scorea++;
scoreView.setText(""+scorea);
}
}
XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.android.courtcounter.MainActivity"
android:orientation="horizontal">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="#android:color/white"
android:background="?attr/colorPrimary"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Teama"
android:textSize="14dp"
android:layout_gravity="center_horizontal"
android:padding="8dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dp"
android:text="#string/zero"
android:layout_gravity="center_horizontal"
android:padding="8dp"
android:id="#+id/team_a_score" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="8dp"
android:text="#string/point3"
android:onClick="displayforpoint3"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="8dp"
android:text="#string/point2"
android:onClick="displayforpoint2"/>
<Button
android:layout_width="match_parent"
android:layout_gravity="center_horizontal"
android:padding="8dp"
android:layout_height="wrap_content"
android:text="#string/freethrow"
android:onClick="displayforfreethrow"/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Teama"
android:textSize="14dp"
android:layout_gravity="center_horizontal"
android:padding="8dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dp"
android:layout_gravity="center_horizontal"
android:padding="8dp"
android:text="#string/zero"
android:id="#+id/team_b_score" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="8dp"
android:text="#string/point3"
android:onClick="displayforpointb3"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="#string/point2"
android:layout_weight="1"/>
android:onClick="displayforpointb2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="8dp"
android:text="#string/freethrow"
android:onClick="displayforfreethrowb"/>
</LinearLayout>
</LinearLayout>

First of all you should define your TextView as a private attributes in your class as follows
private TextView scoreViewA;
private TextView scoreViewB;
Then in your onCreate() method, you should find the view of those TextView as follows
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
scoreViewA = findViewById(R.id.team_a_score);
scoreViewB = findViewById(R.id.team_b_score);
}
Then in your onClick methods, you should update those TextView
public void displayforpointb3(View view){
scoreb+=3;
scoreViewB.setText(""+scoreb);
}
public void displayforpointb2(View view){
scoreb+=2; //You had scoreb+=3 I changed it to 2
scoreViewB.setText(""+scoreb);
}
public void displayforfreethrowb(View view){
scoreb++;
scoreViewB.setText(""+scoreb);
}
public void displayforpoint3(View view){
scorea+=3;
scoreViewA.setText(""+scorea);
}
public void displayforpoint2(View view){
scorea+=2;
scoreViewA.setText(""+scorea);
}
public void displayforfreethrow(View view){
scorea++;
scoreViewA.setText(""+scorea);
}
Also I see something wrong in your activity_main.xml I guess is a misstype
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="#string/point2"
android:layout_weight="1"/> <--- Remove this "/>"
android:onClick="displayforpointb2"/>
Happy coding

Problem
The orientation of the root LinearLayout is horizontal. This results in child views appearing to the right of the Toolbar instead of underneath it.
Solution
Make the root LinearLayout vertical and wrap the two inner view groups in a horizontal LinearLayout.
<?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">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- Team A -->
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Text & Buttons -->
</LinearLayout>
<!-- Team B -->
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Text & Buttons -->
</LinearLayout>
</LinearLayout>
</LinearLayout>
It's not pretty, but it gets the job done. Consider using ConstraintLayout as an alternative to nested view groups if you encounter any performance issues.

Related

Show/Hide Layout onTouch in framelayout

I have a full image fragment. There is a RelativeLayout and LinearLayout in framelayout. LinearLayout contain viewpager.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayoutViewpager">
<com.bogdwellers.pinchtozoom.view.ImageViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<RelativeLayout
android:id="#+id/relativelayoutforbuttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/save_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
</RelativeLayout>
I want When user first open it it should only show image(Viewpager layout) and if user click on it that show other option like - share and save(RelativeLayout).
We can find this type of feature in facebook and whatsapp.
You can simply hide or unhide the relativeLayout's instance using setVisibility.(..) method on linearLayout or imageviewpager click.
layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayoutViewpager">
<ImageViewPager
android:id="#+id/viewpager"
android:onClick="onPagerClick"
android:layout_width="200dp"
android:layout_height="200dp" />
</LinearLayout>
<RelativeLayout
android:id="#+id/relative_layout"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/save_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onSave"
android:text="Save" />
</RelativeLayout>
</FrameLayout>
Activity:
public class TestActivity extends AppCompatActivity {
private boolean isButtonShown = false;
ImageViewPager viewPager;
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
viewPager = findViewById(R.id.viewpager);
layout = findViewById(R.id.relative_layout);
}
public void onPagerClick(View view) {
if (isButtonShown) {
isButtonShown = false;
layout.setVisibility(View.GONE);
} else {
isButtonShown = true;
layout.setVisibility(View.VISIBLE);
}
}
}
Try this type code for visiablity ..
private void share(){
relativeLayout.setVisibility(View.VISIBLE);
linearLayoutViewpager.setVisibility(View.GONE);
}
linearLayoutViewpager=findViewById(R.id.linearLayoutViewpager);
linearLayoutViewpager.setVisibility(View.VISIBLE);
relativeLayout=findViewById(R.id.relativelayoutforbuttons);
relativeLayout.setVisibility(View.GONE);
it only sample code but mange according your needs.
final boolean flag=true;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (flag){
relativeLayout.setVisibility(View.VISIBLE);
flag=false;
}
else{
relativeLayout.setVisibility(View.GONE);
}
}
});

Android: fitsSystemWindows and newline interfering with bottomSheets

I have found out that if the parent layout contains android:fitsSystemWindows="true", it will interfere with my BottomSheets positioning, when a view-related action happens.
Specifically the one I'm encountering: where a newline in a textview will trigger the bottomsheets to offset by the height of the system/notif-bar.
newline + fitsSystemWindows = shoves my bottomsheet down
I eliminated all irrelevant stuff down to buttons, textview and the bottomsheet.
Button2: setText("1\n2") is where the magic happens
As soon as I remove android:fitsSystemWindows="true", it's all ok, no more strange behavior, but I lose the effect of how fitsSystemWindows colors in the system/notif-bar.
I also tried to give my bottomSheet layout its own android:fitsSystemWindows="false", but it has had no effect.
How can I achieve both fitsSystemWindows=true without this strange offset behavior on my bottomSheets?
See for yourself!
public class Test extends AppCompatActivity {
private BottomSheetBehavior bottomSheetBehavior;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_test);
LinearLayout bottomSheet = (LinearLayout) findViewById(R.id.root_btmsheet);
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
bottomSheet.post(new Runnable() {
#Override
public void run() {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
});
((Button) findViewById(R.id.btn1)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
((TextView) findViewById(R.id.info1)).setText("1");
}
});
((Button) findViewById(R.id.btn2)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// the culprit, Mr. Newline
((TextView) findViewById(R.id.info1)).setText("1\n2");
}
});
}
}
act_test.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" <-- the other culprit, Ms. Fits
tools:context=".Test">
<!--<include layout="#layout/act_test_content" />-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:orientation="vertical"
android:id="#+id/root_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
</LinearLayout>
<TextView
android:id="#+id/info1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e0e0e0" />
</LinearLayout>
<!--<include layout="#layout/act_test_btmsheet" />-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
app:layout_behavior="#string/bottom_sheet_behavior"
android:orientation="vertical"
app:behavior_hideable="false"
app:behavior_peekHeight="50dp"
android:background="#5533b5e5"
android:id="#+id/root_btmsheet">
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
I too had a similar problem even with support library 25.1.0. But I later figured out that it could have been because of not using a CollapsingToolbarLayout. I included it and bang!.. it is behaving normally even after any layout changes due to linebreaks

Android toolbar change layout

I got an activity with many fragments. When i change fragments i need to change also and my toolbar declared in MainActivity.class. My problem is that i include a layout and didnt find a method to change this layout. How to change toolbars layout in this case?
MainActivity.class
#ContentView(R.layout.activity_main)
public class MainActivity extends RoboActionBarActivity {
#InjectView(R.id.tool_bar)
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setToolBar();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new SplashFragment())
.commit();
} else {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new HomeFragment())
.commit();
}
}
private void setToolBar() {
setSupportActionBar(toolbar);
}
#Override
public void onAttachFragment(Fragment fragment) {
super.onAttachFragment(fragment);
String currentFragmentClass = fragment.getClass().getSimpleName();
if (currentFragmentClass.equals(getString(R.string.info_fragment))) {
//here i need to change and set onclicks
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".activities.MainActivity">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar" />
<View
android:id="#+id/shadow_view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/tool_bar"
android:background="#ad8c22" />
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/shadow_view"
tools:context=".activities.MainActivity"
tools:ignore="MergeRootFrame" />
</RelativeLayout>
tool_bar.xml
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorToolBar"
>
<RelativeLayout
android:id="#+id/toolbar_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="toggleSlidingMenu"
android:src="#mipmap/icon_sliding_menu" />
<ImageView
android:id="#+id/app_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#mipmap/jammboree" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/app_logo"
android:padding="5dp">
<ImageView
android:id="#+id/hotlist_bell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:contentDescription="bell"
android:gravity="center"
android:padding="7dp"
android:src="#mipmap/icon_my_cart" />
<TextView
android:id="#+id/hotlist_hot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#id/hotlist_bell"
android:layout_alignTop="#id/hotlist_bell"
android:background="#drawable/rounded_square"
android:gravity="center"
android:minWidth="17sp"
android:padding="2dp"
android:text="2"
android:textColor="#ffffffff"
android:textSize="12sp" />
<TextView
android:id="#+id/myMoneyInMyPocket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/hotlist_bell"
android:text="2000$"
android:textColor="#ffffff"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
If I understood right, you want to change the toolbar whenever you change the Fragment the user is currently seeing, isn´t that right?
There is a very simple way to achieve this trough your XML. Let's say you want three different styled toolbars.
1) First, you create XML files for every toolbar you need.
2) Then you include every toolbar in to your activity_main.xml, like this:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".activities.MainActivity">
<include
android:id="#+id/toolbar1"
layout="#layout/tool_bar1"
android:visibility="visible" />
<include
android:id="#+id/toolbar2"
layout="#layout/tool_bar2"
android:visibility="gone" />
<include
android:id="#+id/toolbar3"
layout="#layout/tool_bar3"
android:visibility="gone" />
// Here is the rest of your XML code
</RelativeLayout>
See how all three toolbar includes have the visibility property?
Well, now you can toy with this property, and show/hide the desired toolbar whenever you want/need to.
For example:
RelativeLayout mLayout = (RelativeLayout) getActivity().findViewById(R.id.my_main_layout);
Toolbar mToolbar1 = (Toolbar) mLayout.findViewById(R.id.toolbar1);
mToolbar1.setVisibility(View.GONE);
Toolbar mToolbar2 = (Toolbar) mLayout.findViewById(R.id.toolbar2);
mToolbar2.setVisibility(View.GONE);
Toolbar mToolbar3 = (Toolbar) mLayout.findViewById(R.id.toolbar3);
mToolbar3.setVisibility(View.VISIBLE);
And just change which one are you making visible, according to your needs.
Hope this helps.
I don`t think the method of #herrmartell is nice;
In my way,I have three Fragment in a Activity,I defined some different layout resources for a common toolbar xml,and load them in toolbar when I click different button; my code :
common_toolbar xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar">
<!-- your custom layout -->
</android.support.v7.widget.Toolbar>
activity_main_toolbar_common_view.xml
<TextView
android:id="#+id/action_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="迪士尼...ˇ"
android:textColor="#color/white"
android:textSize="#dimen/larget_text_size"
android:paddingLeft="10dp"
android:layout_alignParentStart="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
activity_main_toolbar_mine_view.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/mine"
android:textSize="#dimen/big_text_size"
android:textColor="#color/white"
android:gravity="center"
android:layout_centerInParent="true"/>
<ImageView
android:id="#+id/setting"
android:layout_width="#dimen/toolbar_icon_size"
android:layout_height="#dimen/toolbar_icon_size"
android:src="#drawable/toolbar_setting"
android:layout_alignParentRight="true"
/>
switchToolbar() method in Activity:
public void switchToolbar(int layout) {
if(currentToolbarLayout == layout){
return;
}
currentToolbarLayout = layout;
View v = getLayoutInflater().inflate(layout,null);
toolbar.removeAllViews();
toolbar.addView(v);
switch (layout){
case R.layout.activity_main_toolbar_common_view:
v.findViewById(R.id.action_location).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "跳到位置", Toast.LENGTH_SHORT).show();
}
});
break;
case R.layout.activity_main_toolbar_mine_view:
v.findViewById(R.id.setting).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "跳到设置", Toast.LENGTH_SHORT).show();
}
});
break;
}
}
I render my toolbar view in the onCreate() method of Activity:
ButterKnife.bind(this);
setSupportActionBar(toolbar);
switchToolbar(R.layout.activity_main_toolbar_common_view);
when I switch toolbar, I call:
switchToolbar(R.layout.activity_main_toolbar_mine_view);

Action Buttons do not cover the entire Toolbar

I have used a Toolbar to display four action buttons. When I inflate the toolbar with menu, the action buttons are displayed towards one side as shown in the picture :
How can I make these action buttons cover the entire toolbar?
This is the code for the Activity :
public class ResultActivity extends FragmentActivity {
final int TAB_NUM = 2;
private ViewPager mViewPager;
private PagerAdapter mPagerAdapter;
private Toolbar mResultToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
mResultToolbar = (Toolbar) findViewById(R.id.result_toolbar);
mResultToolbar.inflateMenu(R.menu.result_menu);
mResultToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.camera_action :
Intent cameraIntent = new Intent(getApplicationContext(),CameraActivity.class);
startActivity(cameraIntent);
break;
case R.id.gallery_action :
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
break;
case R.id.profile_action :
Toast.makeText(getApplicationContext(),"Profile",Toast.LENGTH_SHORT).show();
break;
case R.id.slide_up_action :
Toast.makeText(getApplicationContext(),"Slider",Toast.LENGTH_SHORT).show();
break;
}
return true;
}
});
mViewPager = (ViewPager)findViewById(R.id.result_view_pager);
mPagerAdapter = new ResultViewPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mPagerAdapter);
mViewPager.setPageTransformer(true, new DepthPageTransformer());
}
}
This is the 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:layout_marginLeft="#dimen/activity_margin_horizontal"
android:layout_marginRight="#dimen/activity_margin_horizontal"
android:background="#color/app_background">
<android.support.v4.view.ViewPager
android:id="#+id/result_view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/result_toolbar"
android:layout_alignParentTop="true"
android:background="#color/colorAccent">
<android.support.v4.view.PagerTabStrip
android:id="#+id/result_page_tab_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#color/instagram_background" />
</android.support.v4.view.ViewPager>
<android.support.v7.widget.Toolbar
android:id="#id/result_toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/toolbar_height"
android:layout_alignParentBottom="true"
android:background="#color/default_line_indicator_selected_color"
android:elevation="#dimen/toolbar_elevation">
</android.support.v7.widget.Toolbar>
</RelativeLayout>
You can add LinearLayout to toolbar with parameters
<LinearLayout android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:orientation="horizontaly"
android:weightSum="4"> </LinearLayout>
then for buttons
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"></Button>
and every button will take 1/4 of toolbar space.
so to sum up, inside toolbar you will have:
<LinearLayout android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:orientation="horizontaly"
android:weightSum="4">
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"></Button>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"></Button>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"></Button>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"></Button>
</LinearLayout>
I wrote it from head, so I not tested this code, but I hope it will be helpful for you.
If you have only four options for menu then you can think of not using Toolbar . Instead you can use a LinearLayout directly in AppBarlayout as it can hold other views then toolbar too.
Like
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/app_background">
<LinearLayout
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:orientation="horizontaly"
android:weightSum="4">
<Button
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"></Button>
<Button
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"></Button>
<Button
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"></Button>
<Button
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"></Button>
</LinearLayout>
</android.support.design.widget.AppBarLayout>
I used the same layout of MyWay. Thanks MyWay.

Use Custom Layout in NavigationDrawer With Header And list

How to do add custom layout in NavigationView and design my create custom NavigationView use material design,i want put my drawer icon to right and text left of it something like this
I Search too much and this is my experience that works fine
at first create layout for header. its name is nav_header_main.xml then put it in layouts folders in res and put this code in it..
<?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="#dimen/nav_header_height"
android:background="#drawable/header"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
android:gravity="top">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/cv_nave_profile_image"
android:layout_width="#dimen/nav_profile_image"
android:layout_height="#dimen/nav_profile_image"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/profile"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/cv_nave_profile_image"
android:layout_alignParentTop="true"
android:padding="#dimen/activity_horizontal_margin"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_nav_name"
android:textStyle="bold"
android:typeface="sans"
android:textColor="#ffffff"
android:gravity="right"
android:layout_gravity="right"
android:text="رخداد جدید"
android:paddingBottom="5dp"
android:textSize="#dimen/body"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:typeface="sans"
android:textColor="#ffffff"
android:id="#+id/tv_nav_phone"
android:layout_alignParentLeft="true"
android:text="0370077315"
/>
</RelativeLayout>
</LinearLayout>
then i include it as child of NavigationView and For menu item i use RecyclerView to show menu and icon so my NavigationView is
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="spydroid.ir.dorobar.Activities.SearchActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_search" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView android:id="#+id/nav_view"
android:layout_width="fill_parent" android:layout_height="match_parent"
android:layout_gravity="right" android:fitsSystemWindows="true"
android:layout_marginLeft="#dimen/nav_margin"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<include layout="#layout/nav_header_main" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/drawer_slidermenu"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp"/>
</RelativeLayout>
</LinearLayout>
</android.support.design.widget.NavigationView>
just you have to remember put your NavigationView in DrawerLayout
then i create layout for menu item with ImageView and TextView this layout and this name is card_drawer_item.xml and its code is here
<?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="48dp">
<ImageView
android:id="#+id/drawer_icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:src="#drawable/ic_about"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/drawer_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toLeftOf="#id/drawer_icon"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:typeface="sans"
android:paddingRight="40dp"/>
</RelativeLayout>
then i create ViewHolder folder for this layout.
public class DrawerItemHolder extends RecyclerView.ViewHolder {
public ImageView itemIcon;
public TextView itemText;
public DrawerItemHolder(View itemView) {
super(itemView);
itemIcon= (ImageView) itemView.findViewById(R.id.drawer_icon);
itemText= (TextView) itemView.findViewById(R.id.drawer_text);
}
}
now i define text of menu items as string array and array that contains menu icons in menu in strings.xml
<string-array name="drawer_items">
<item>setting</item>
<item>add record</item>
<item>ads</item>
<item>about</item>
<item>call</item>
<item>help</item>
<item>privacy</item>
</string-array>
<array name="drawers_icons">
<item>#drawable/ic_action_settings</item>
<item>#drawable/ic_plus</item>
<item>#drawable/ic_ads</item>
<item>#drawable/ic_about</item>
<item>#drawable/ic_phone</item>
<item>#drawable/ic_help</item>
<item>#drawable/ic_policy</item>
</array>
then we just need an Adapter like this
public class DrawerItemAdapter extends RecyclerView.Adapter<DrawerItemHolder> {
// slide menu items
private List<DrawerItem> items;
private List<Integer> drawerIcons;
public DrawerItemAdapter(List<DrawerItem> items) {
super();
this.items = items;
}
#Override
public DrawerItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.card_drawer_item, parent, false);
return new DrawerItemHolder(itemView);
}
#Override
public void onBindViewHolder(DrawerItemHolder holder, int position) {
holder.itemIcon.setImageResource(items.get(position).getIconId());
holder.itemText.setText(items.get(position).getText());
}
#Override
public int getItemCount() {
return items.size();
}
}
every thing is ok .. just now we have to set NavigationView in Activity.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
recList = (RecyclerView) findViewById(R.id.drawer_slidermenu);
recList.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recList.setLayoutManager(llm);
String []itemsTitle=getResources().getStringArray(R.array.drawer_items);
TypedArray icons=getResources().obtainTypedArray(R.array.drawers_icons);
List<DrawerItem>drawerItems= new ArrayList<DrawerItem>();
for(int i=0;i<itemsTitle.length;i++){
drawerItems.add(new DrawerItem(icons.getResourceId(i,-1),itemsTitle[i]));
}
DrawerItemAdapter ad= new DrawerItemAdapter(drawerItems);
recList.setAdapter(ad);
}
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.END)) {
drawer.closeDrawer(GravityCompat.END);
return;
}
super.onBackPressed();
}

Categories

Resources