custom scrollview not appearing - android

I have a custom ScrollView that works fine if it is the only thing in the emulator.
For example, this works fine:
public class Timeline2Activity extends Activity {
private TimelineView tlv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tlv = new TimelineView(this);
setContentView(tlv);
}
}
But if I first add widgets then it fails to appear. I've searched and experimented for 2 days and nothing works.
Here is the class declaration of the custom ScrollView:
public class TimelineView extends ScrollView implements OnTouchListener {
And here are the constructors:
public TimelineView(Context context) {
super(context);
this.setOnTouchListener(this);
setVisibility(VISIBLE);
}
//the following constructor is needed to inflate the view from XML
public TimelineView(Context context, AttributeSet ats) {
super(context,ats);
this.setOnTouchListener(this);
setVisibility(VISIBLE);
}
Here is the main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/zoomall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Zoom All" />
<Button
android:id="#+id/zoomout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
<Button
android:id="#+id/zoomin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
<Spinner
android:id="#+id/category_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/category_prompt"
/>
</LinearLayout>
<com.projects.timeline2.TimelineView
android:id="#+id/timeline_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
And here is the main program:
public class Timeline2Activity extends Activity {
private TimelineView tlv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//tlv = new TimelineView(this);
//setContentView(tlv);
setContentView(R.layout.main);
Spinner spinnerCategory = (Spinner) findViewById(R.id.category_spinner);
ArrayAdapter<CharSequence> adapterCategory = ArrayAdapter.createFromResource(
this, R.array.categories, android.R.layout.simple_spinner_item);
adapterCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCategory.setAdapter(adapterCategory);
spinnerCategory.setOnItemSelectedListener(new MyOnItemSelectedListener());
//TimelineView tlv = (TimelineView) findViewById(R.id.timeline_view);
//ViewGroup container = (ViewGroup) findViewById(R.id.container);
//container.addView(tlv);
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(), "The selection is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
}
The lines commented out are stuff I've tried but didn't work either.
This code correctly puts 3 buttons and a spinner in a horizontal row across the top. But TimelineView fails to appear below them. I know that TimelineView is running because I can see debugging output in the LogCat that I put in.
I'm at my wits end and hope someone can shed some light. This seems like a common fundamental need and sure deserves a google tutorial.

You are using LinearLayout and you have used android:layout_height="fill_parent" to the first element so the first element will fill the whole screen height and there will be no space for the second element (com.projects.timeline2.TimelineView) thats why you are unable to see second element.
Use Relative Layout instead. and mention android:layout_alignParentBottom="true" for second element so that it will always stay to the buttom of the screen and align the first element to the above of second by putting android:layout_above to first element
Your XML will be
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.projects.timeline2.TimelineView
android:id="#+id/timeline_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_above="#+id/timeline_view">
<Button
android:id="#+id/zoomall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Zoom All" />
<Button
android:id="#+id/zoomout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
<Button
android:id="#+id/zoomin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
<Spinner
android:id="#+id/category_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="category_prompt"
/>
</LinearLayout>
</RelativeLayout>

I think you should change TimeLine's layout height to fill_parent in xml, or add some content to it.

I finally got it. Here is the main.xml that worked.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/buttons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="true"
>
<Button
android:id="#+id/zoomall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Zoom All" />
<Button
android:id="#+id/zoomout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
<Button
android:id="#+id/zoomin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
<Spinner
android:id="#+id/category_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<com.projects.timeline2.TimelineView
android:id="#+id/timeline_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_below="#id/buttons"
/>
</RelativeLayout>
Thanks everyone for getting me going in the right direction!

Related

Visibility changes "by itself"

I have this ListView with an Adapter that extends BaseAdapter. Inside the
public View getView(final int position, View view, ViewGroup parent) {...}
I attach a clickListener to view like this:
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final LinearLayout llMatchInfo = v.findViewById(R.id.matchInfo);
if (llMatchInfo == null) return;
llMatchInfo.setVisibility(llMatchInfo.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
}
});
Then I have this one user complaining that when he clicks an element llMatchInfo is visible for a split second and then disappears again.
This is the only place the matchInfo view has it's visibility changed.
Can anyone help me figure out why this is happening?
Thanks in advance
Here's the outer XML for each item in my ListView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingEnd="5dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="10dp"
android:layout_height="fill_parent"
android:id="#+id/live_indicator">
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/match_data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
...
</LinearLayout>
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/match_info"
android:id="#+id/matchInfo" />
</LinearLayout>
</LinearLayout>
And the inner XML for the actual matchInfo (the part I want to toggle visibility on):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<LinearLayout
android:id="#+id/team_logos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<ImageView
... />
<ImageView
... />
</LinearLayout>
<TextView
... />
<TextView
... />
<TextView
... />
<LinearLayout
android:id="#+id/tv_channels"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:visibility="visible">
<ImageView
... />
<ImageView
... />
</LinearLayout>
</LinearLayout>

Custom listview item xml disabling onitemclicklistener

i am using below xml for my ListView item layout
<?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:gravity="center_vertical"
android:orientation="horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="16dp">
<ImageView
android:id="#+id/contact_item_icon"
android:layout_width="50dp"
android:layout_height="50dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="#+id/contact_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Feel The Light" />
<TextView
android:id="#+id/contact_item_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_audio_call_dark"
android:id="#+id/btn_audio_call"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_video_call_dark"
android:id="#+id/btn_video_call"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="Invite"
android:background="#drawable/selector_button_green_oval"
android:textColor="#color/white"
android:id="#+id/btn_invite"/>
</RelativeLayout>
</LinearLayout>
i know in this xml some views are overlapping each other but in my custom array adapter i make visibility to gone for some of them based on certain condition, but what this xml do is to disable my ListView onItemClickListener but if i exclude this from xml then onItemClickListener works fine but i want to make onItemClickListener work without removing xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_audio_call_dark"
android:id="#+id/btn_audio_call"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_video_call_dark"
android:id="#+id/btn_video_call"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="Invite"
android:background="#drawable/selector_button_green_oval"
android:textColor="#color/white"
android:id="#+id/btn_invite"/>
</RelativeLayout>
In your adapter getView(), set onClickListener to each button.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do something
}
}
If you want to handle click event for entire list item then you need to set onClickListener to root LinearLayout of your listViewItem XML:
ListViewItem XML: Give an id to 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="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="16dp"
android:id="#+id/container">
<ImageView
android:id="#+id/contact_item_icon"
android:layout_width="50dp"
android:layout_height="50dp"/>
.................
...................
Adapter: Set OnClickListener to LinearLayout
#Override
public View getView(int position, View convertView, ViewGroup parent) {
.............
.................
LinearLayout container = (LinearLayout) convertView.findViewById(R.id.container);
container.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do something
}
}
.................
...................
}
Hope this will help!

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;
}
}
}

How to shift a LinearLayout when a SlidingDrawer expands?

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.

Change the height from a RelativeLayout inside costumeadapter

Hey guys I'm newbie at Android development, and I would like to build a sub item for each listview item.
Just like this:
ListView 1
sub item 1
ListView 1
sub item 1
I though that it would be esier to create a new relative layout for each listview item, so my XML file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/wrap_layout"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/first_col"
android:layout_width="fill_parent"
android:layout_height="200dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
<ImageView
android:id="#+id/bg_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/overlay_bg" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="9dp" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/current_event"
android:layout_alignParentLeft="true"
android:layout_marginBottom="6dp"
android:letterSpacing="1.2"
android:shadowColor="#color/dark_grey"
android:shadowDx="-2"
android:shadowDy="2"
android:shadowRadius="0.01"
android:textColor="#color/white"
android:textSize="22sp" />
<ImageView
android:layout_width="17dp"
android:layout_height="17dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/persons" />
<TextView
android:id="#+id/current_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/name"
android:layout_alignParentBottom="true"
android:text="TextView"
android:textColor="#color/grey"
android:textSize="13sp" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/second_col"
android:layout_width="fill_parent"
android:layout_marginTop="200dp"
android:layout_height="200dp" >
<TextView
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="24dp"
android:text="TextView" />
</RelativeLayout>
</RelativeLayout>
Each item is divided by two columns, the second column should be hidden column, thats why I changed the layout height to 200dp.
And to show the full layout, I'm doing that:
public View getView(final int position, View convertView, ViewGroup parent) {
// here you let SimpleAdapter built the view normally.
final View v = super.getView(position, convertView, parent);
final RelativeLayout lebg = (RelativeLayout) v.findViewById(R.id.wrap_layout);
lebg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View b) {
lebg.getLayoutParams().height = 400;
}
});
return v;
}
I just need to change the height from 200 to 400 to show the full Listview item.
The issue:
Is not working that well because when I click at the listview it only changes the height when I scroll...
Is there any better way to do what I am trying?
Thanks.
When you change LayoutParams you have to call requestLayout to see changes:
lebg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View b) {
lebg.getLayoutParams().height = 400;
requestLayout();
}
});

Categories

Resources