How to shift a LinearLayout when a SlidingDrawer expands? - android

I am trying to implement a panel in a fragment that can expand on button click from the right side of the screen. I can't use the navigation drawer, because I already have navigation drawers from both left and right sides.
The idea is to achieve something like that:
I was almost able to do it with the SlidingDrawer widget (it's deprecated..), the only problem is that I don't know how to make the LinearLayout to appear in the middle and then to shift when the button to expand the SlidingDrawer is clicked.
I have the following code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button handle = (Button) findViewById(R.id.handle);
SlidingDrawer drawer = (SlidingDrawer) findViewById(R.id.slidingDrawer);
drawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
#Override
public void onDrawerOpened() {
}
});
drawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
#Override
public void onDrawerClosed() {
}
});
}
}
And the XML code:
<?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="com.test.slidingdrawertest.slidingdrawertest.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST TEST TEST" />
</LinearLayout>
<LinearLayout
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<SlidingDrawer
android:id="#+id/slidingDrawer"
android:layout_width="200dp"
android:layout_height="400dp"
android:layout_marginTop="100dp"
android:layout_gravity="end"
android:content="#+id/content"
android:handle="#+id/handle"
android:orientation="horizontal">
<Button
android:id="#+id/handle"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Expand" />
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST TEST TEST" />
</LinearLayout>
</SlidingDrawer>
</LinearLayout>
</RelativeLayout>

You can edit your 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"
android:padding="10dp"
tools:context="com.test.slidingdrawertest.slidingdrawertest.MainActivity">
<LinearLayout
android:id="#+id/left" // this is your linear layout
android:layout_width="match_parent" // that you want to shift left
android:layout_height="match_parent"
android:layout_toLeftOf="#+id/right" // apply this property due to
android:layout_alignParentLeft="true" //which if the width of sliding menu
android:gravity="center" //increase it will automatically compressed
android:background="#aa00aa"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST TEST TEST" />
</LinearLayout>
<LinearLayout
android:id="#+id/right"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:background="#0000aa"
android:layout_height="match_parent">
<SlidingDrawer
android:id="#+id/slidingDrawer"
android:layout_width="200dp" // you can check by increasing or decreasing the with of this slidingdrawer
android:layout_height="400dp"
android:layout_marginTop="100dp"
android:layout_gravity="end"
android:content="#+id/content"
android:handle="#+id/handle"
android:orientation="horizontal">
<Button
android:id="#+id/handle"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Expand" />
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST TEST TEST" />
</LinearLayout>
</SlidingDrawer>
</LinearLayout>
</RelativeLayout>
apart from this if set any layout to the right of your linear layout which is initially GONE and as you click on the button its visibility changed to INVISIBLE/VISIBLE so it will shift the linear layout to left. Hope that helps!

If you want a little exemple here is my solution: (I don't know if Avani Nagar's solution works or not):
TextView res;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView img = (TextView)findViewById(R.id.second);
res = (TextView)findViewById(R.id.first);
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
img.setX(img.getX() - 5);
changew(img.getX());
}
});
}
private void changew(float w){
res.getLayoutParams().width = (int)(res.getX() +w);
res.requestLayout();
res.invalidate();
}
The 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:background="#android:color/black">
<TextView
android:layout_toRightOf="#+id/first"
android:text="bonjourrrrrr"
android:id="#+id/second"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:background="#android:color/holo_red_dark" />
<TextView
android:text="salutttttttt"
android:id="#id/first"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:background="#android:color/holo_green_light" />
</RelativeLayout>

I managed to do it, it actually wasn't so difficult, but it's a little tricky. Here is the solution:
The xml code:
<?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"
android:id="#+id/topLayout"
tools:context="com.test.slidingdrawertest.slidingdrawertest.MainActivity">
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST" />
</LinearLayout>
<LinearLayout
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<SlidingDrawer
android:id="#+id/slidingDrawer"
android:layout_width="200dp"
android:layout_height="400dp"
android:layout_marginTop="100dp"
android:layout_gravity="end"
android:content="#+id/content"
android:handle="#+id/handle"
android:orientation="horizontal">
<Button
android:id="#+id/handle"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Expand" />
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST TEST TESTT" />
</LinearLayout>
</SlidingDrawer>
</LinearLayout>
</RelativeLayout>
The code in MainActivity.java:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button handle = (Button) findViewById(R.id.handle);
SlidingDrawer drawer = (SlidingDrawer) findViewById(R.id.slidingDrawer);
final View k = findViewById(R.id.mainLayout);
drawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
#Override
public void onDrawerOpened() {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
k.setLayoutParams(params);
}
});
drawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
#Override
public void onDrawerClosed() {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
k.setLayoutParams(params);
}
});
}
}
And the explanation: I had to give main main layout an id (in XML) and then find it in the code behind, then I had to apply new parameters to it when the drawer opens or closes. Basically, we need to find the parent layout and set the parameters to it.
Now the LinearLayout is able to shift it's position on drawer open, and it goes back to it's original position on drawer close. Next step is to make this process with an animation, so it's smooth and doesn't just jump back and forth to the new positions.

Related

Reverse layouts inside RelativeLayout programmatically

I have one relativeLayout and three view's inside RelativeLayout. There views are place on each other.
<RelativeLayout
android:id="#+id/includelayoutgroup"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/transparentColorView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clickable="true"
android:visibility="visible" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/keyboard_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible">
<include
layout="#layout/transfer_keyboard_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/finalRequestView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clickable="true"
android:visibility="visible">
<include
layout="#layout/fragment_transfer_new_version_container_child"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</RelativeLayout>
Is it a possible to reverse position my view's? I mean,in my code last Layout is finalRequestView and in button click i would to my last view in first position,and other view's place on my view,
How i can solve this problem?
I try to reverse layout by following demo & it works as you want, please take a look
ReverseLayout.java
public class ReverseLayout extends AppCompatActivity {
LinearLayout llRoot;
Button btReverseLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reverse_layout);
llRoot = (LinearLayout) findViewById(R.id.llRoot);
btReverseLayout = (Button) findViewById(R.id.btReverseLayout);
btReverseLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<View> alViews = new ArrayList<View>();
for (int i = llRoot.getChildCount() - 1; i >= 0; i--) {
View view = llRoot.getChildAt(i);
llRoot.removeViewAt(i);
alViews.add(view);
}
for (int j = 0; j < alViews.size(); j++) {
llRoot.addView(alViews.get(j));
}
}
});
}
}
reverse_layout.xml layout file
<?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:id="#+id/activity_6"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.tosc185.testproject.Activity6">
<LinearLayout
android:id="#+id/llRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF0000"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FF00"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0000FF"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/btReverseLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Reverse Layout" />
</RelativeLayout>

I want current layout to be changed so that I can scroll entire layout.

<LinearLayout
android:id="#+id/layout_question_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="55dp"
>
<Button
android:id="#+id/btn_question_backButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/backicon_round"/>
<TextView
android:id="#+id/text_question_detail_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#color/color_green"
android:text="Error"
android:textSize="19dp"
/>
</RelativeLayout>
<ImageView
android:id="#+id/image_question_detail_image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:scaleType="centerCrop"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="2">
<TextView
android:id="#+id/text_question_detail_userComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:layout_marginBottom="25dp"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3">
<ListView
android:id="#+id/list_question_detail_expComments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#android:color/transparent"
android:dividerHeight="10dp"></ListView>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
Now, I set layout for title on the top. Then, a Imageview, a textview and a listview follow below this layout, and only 1 textview is inside listview.
listview can vary in size.
The problem is, if the size of the listview is very big, I can only scroll the screen assigned to listview.
But, I want scroll the entire screen.
To solve this problem, I added scrollview outside of the first linearlayout.
However, It didn't work.(the imageview disappeared)
What can I do?
you can make your listview addheaderview,and put your imageview and textview into headerview
yourlayout.xml
<?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:id="#+id/layout_question_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="55dp">
<Button
android:id="#+id/btn_question_backButton"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:id="#+id/text_question_detail_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Error"
android:textColor="#000000"
android:textSize="19dp"
/>
</RelativeLayout>
<ListView
android:id="#+id/list_question_detail_expComments"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#android:color/transparent"
android:dividerHeight="10dp"></ListView>
lv_header.xml
<?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="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="#+id/image_question_detail_image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/text_question_detail_userComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:textSize="20dp" />
Activity.class
public class MainActivity extends AppCompatActivity {
private ListView list_question_detail_expComments;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list_question_detail_expComments = (ListView) findViewById(R.id.list_question_detail_expComments);
View lvHeaderView = View.inflate(this,R.layout.lv_header,null);
list_question_detail_expComments.addHeaderView(lvHeaderView);
list_question_detail_expComments.setAdapter(new LvAdapter());
}
private class LvAdapter extends BaseAdapter{
#Override
public int getCount() {
return 20;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
TextView tv = new TextView(MainActivity.this);
tv.setText("test"+i);
return tv;
}
}
}

Android sliding drawer from top to bottom

I want to create sliding drawer in android which should be placed on top of the activity and should be open from top to bottom like android notification panel. How to achieve this?
The default SlidingDrawer class doesn't allow this. You can use the Panel class from here to get something very similar though OR set the rotation of 180ยบ for the SlidingDrawer, the content and the handle. Note : android:rotation is support on API > Level 11
http://www.ohloh.net/p/android-misc-widgets
https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/SlidingDrawer-Topdown
Reference
If you want to have sliding drawer from top to bottom then just copy pase this code in your project, it is 100% working code.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#d3d3d3"
>
<SlidingDrawer
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/slidingDrawer"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:content="#+id/content"
android:gravity="center_horizontal"
android:handle="#+id/handle"
android:orientation="vertical"
android:rotation="180" >
<LinearLayout
android:id="#+id/handle"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:gravity="right"
android:background="#+drawable/drawer_bk"
>
<ImageView
android:id="#+id/imageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#+drawable/drawer1" />
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="45dp" >
<RelativeLayout
android:id="#+id/rel1"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:rotation="180" >
<ImageView
android:id="#+id/ImageView04"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="visit"
android:src="#drawable/visit_icon" />
<ImageView
android:id="#+id/ImageView03"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_marginRight="35dp"
android:layout_toLeftOf="#+id/ImageView04"
android:onClick="contact_us"
android:src="#drawable/contact_icon" />
<ImageView
android:id="#+id/ImageView02"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="35dp"
android:layout_toRightOf="#+id/ImageView04"
android:onClick="logout"
android:src="#drawable/singout_icon" />
</RelativeLayout>
</RelativeLayout>
</SlidingDrawer>
MainActivity.java
public class MainActivity extends Activity
{
/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 4000;
Button slideButton,b1, b2,b3,b4;
SlidingDrawer slidingDrawer;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//slideButton = (Button) findViewById(R.id.slideButton);
slidingDrawer = (SlidingDrawer) findViewById(R.id.slidingDrawer);
slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
#Override
public void onDrawerOpened()
{
// slideButton.setBackgroundResource(R.drawable.down_arrow_icon);
slidingDrawer.setBackgroundResource(R.drawable.hd_img_1);
}
});
slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener()
{
#Override
public void onDrawerClosed()
{
// slideButton.setBackgroundResource(R.drawable.upwar_arrow_icon);
slidingDrawer.setBackgroundColor(Color.TRANSPARENT);
}
});
}
}

How can I display the layout containing the buttons down on the screen

With the layout below the buttons are being shown on top of the page. How can I make them appear below, after the container, down on the page?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/container"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_vertical|center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/Main"
android:onClick="ClickHomePage"
android:text= "#string/Home"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/map"
android:onClick="ClickClients"
android:text= "#string/Clients"
>
</Button>
</LinearLayout>
</LinearLayout>
The MainActivity looks as follows and the HomePage.xml is also below
public class MainActivity extends ActionBarActivity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GetButtonClicked(5);
setContentView(R.layout.activity_main);
}
public void GetButtonClicked(int position)
{
// update the main content by replacing fragments
Fragment fragment = null;
switch (position){
case 0:
fragment= new FirstPageFragment();
break;
default:
fragment = new FirstPageFragment();
break;
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
public void ClickHomePage(View view){
GetButtonClicked(0);
}
public void ClickClients(View view){
GetButtonClicked();
}
}
<FrameLayout 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="com.test.FirstPageFragment" android:background="#drawable/theme">
</FrameLayout>
This is the FirstPage
public class FirstPageFragment extends Fragment {
public FirstPageFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first_page, container, false);
}
}
Use ralative layout insted of linear.
Make a different layout for bottom buttons, and then at the last of your container layout use this:
<include android:id="#+id/bottomLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
layout="#layout/bottom_buttons"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:id="#+id/Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClickHomePage"
android:text="#string/Home" />
<Button
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClickClients"
android:text="#string/Clients" >
</Button>
</LinearLayout>
Try this
Use RelativeLayout with android:layout_alignParentBottom="true" step by step guide is available here.
Your skeleton for layout should look like this
<RelativeLayout
<LinearLayout android:layout_alignParentBottom="true">
<Button
<Button
</LinearLayout>
</RelativeLayout>
Final Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClickHomePage"
android:text="Home" />
<Button
android:layout_marginLeft="30dp"
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClickClients"
android:text="Clients" >
</Button>
</LinearLayout>
The issue was that I did not include the container in another layout...
<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="com.test.MainPageActivityActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="#+id/container">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_alignParentTop="#+id/container">
and insert buttons layout here....
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/Main"
android:onClick="ClickHomePage"
android:text= "#string/Home"
/>

Button inside DrawerLayout is not clickable

I've been searching for a while with this issue and i cant find solution yet.
For my design of DrawerLayout, I will not be using listview but linearlayout as container for my views. I test it first by adding a button inside the linearlayout but i cant click it.
Heres the XML.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/left_drawer"
android:background="#color/white"
android:orientation="vertical"
android:layout_gravity="start"
android:layout_width="250dp"
android:layout_height="match_parent">
<Button
android:id="#+id/btn_test"
android:text="TEST"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<FrameLayout
android:id="#+id/container_fragment2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="1dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
>
</FrameLayout>
In java, I also tried to put text in the button once it is being loaded and its working.The only thing that is not working is when i click it. the drawer will just close.
/**Init Drawer*/
private void initDrawer(){
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerContainer = (LinearLayout) findViewById(R.id.left_drawer);
//mDrawerContainer.setOnClickListener(new DrawerItemClickListener());
mDrawerContainer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context,"initDrawer",Toast.LENGTH_SHORT).show();
}
});
btn_test = (Button)mDrawerContainer.findViewById(R.id.btn_test);
btn_test.setOnClickListener(new View.OnClickListener() { //this is not working,i cant click the button
#Override
public void onClick(View v) {
Log.e("MyActivity","test");
Toast.makeText(context,"test",Toast.LENGTH_SHORT).show();
}
});
btn_test.setText("hooray"); //this one is working,it change the text to hooray once loaded
}
Im looking forward for your input guys,
Thanks a lot.
Its so ironic, I just change the positions of my layout. I transferred the linearlayout below the fragment and its now working.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/container_fragment2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="1dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
>
</FrameLayout>
<LinearLayout
android:id="#+id/left_drawer"
android:background="#color/white"
android:orientation="vertical"
android:layout_gravity="start"
android:layout_width="250dp"
android:layout_height="match_parent">
<Button
android:id="#+id/btn_test"
android:text="TEST"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Check this ! reference
you have to set a TouchListener for your drawer layout.
eg/
gestureDetector = new GestureDetector(this, new CustomGestureListener());
mDrawerLayout.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
});
Remove the OnClickListener that you've set on the DrawerLayout itself.
check it----
cast your button with layout where u keep the button
here is my xml
<RelativeLayout
android:id="#+id/whatYouWantInLeftDrawer"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#EBF4FA">
<RelativeLayout
android:id="#+id/top_view_rel"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#color/blue_color">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:visibility="visible">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/drawer_driver_name"
android:textColor="#color/text_color"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/name_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/name"
android:layout_marginLeft="135dp"
android:text="name"
android:textAllCaps="true"
android:textColor="#color/text_color"
android:textSize="16sp"
android:visibility="gone" />
<Button
android:id="#+id/edit"
android:layout_width="30dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:background="#color/colorTransparent"
android:drawableRight="#drawable/edit1"
/>
in activity--
leftDrawerRel = (RelativeLayout)findViewById(R.id.whatYouWantInLeftDrawer);
edit= (Button)leftDrawerRel.findViewById(R.id.edit);
edit.setOnClickListener(this);

Categories

Resources