Sliding Drawer close on back button pressed - android

I have a sliding drawer in my application.
It open when I click on android:handle, which I have given to an image view.
I want to close the drawer when back button is pressed.
Code:
SlidingDrawer slider;
#Override
public void onBackPressed() {
slider.close ();
}
super.onBackPressed();
Main. Xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
android:orientation="vertical" >
<SlidingDrawer
android:id="#+id/slider"
android:layout_width="150dp"
android:layout_marginRight="0dp"
android:layout_height="match_parent"
android:handle="#+id/handle"
android:content="#+id/content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/handle"
android:background="#drawable/up"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="center"
/>
<GridView
android:id="#+id/content"
android:background="#color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</SlidingDrawer>
</LinearLayout>
</RelativeLayout>
Activity file
#Override
public void onCreate(Bundle savedInstanceState)
{
SlidingDrawer slide = (SlidingDrawer)this.findViewById(R.id.slider);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override public void onBackPressed() {
if(slide.isOpened()){
slide.close ();
}
else{
super.onBackPressed();
}
}
}

You only need to close it if it's actually opened:
SlidingDrawer slider;
#Override
public void onBackPressed() {
if (slider.isOpened()) {
slider.close ();
} else {
super.onBackPressed();
}
}
EDIT
your slider field is out of scope. You need to declare it as a class field in order it can be accessed by all non-static methods. Also consult this document.

You don't call the super method inside the in on back pressed method you just give this only...
#Override
public void onBackPressed() {
slider.close ();
}
Don't call Super method inside the back pressed

Related

ListView cannot show after being invsibile during onCreate

I have problem I want to hide the listVeiw during onCreate,but the problem is that when I hide my listview to invisible, the setOnTouchListener will not work.but if I remove lstv.setVisibility(View.INVISIBLE); before the setOnTouchListener, the setOnTouchListener is working,but I need to hide first my listview during start up or onCreate.
Thank you in advance.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lstv.setVisibility(View.INVISIBLE);
vdo.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
lstv.setVisibility(View.VISIBLE);
lstv.postDelayed(new Runnable() {
#Override
public void run() {
lstv.setVisibility(View.INVISIBLE);
}
}, 3000);
return true;
}
});
}
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
tools:context=".MainActivity" >
<VideoView
android:id="#+id/vdo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listview"
android:layout_width="229dp"
android:layout_height="fill_parent"
android:smoothScrollbar="true"
android:soundEffectsEnabled="true"
/>
</FrameLayout >
For hide ListView and receive onTouchEvent on vdo
You should change
lstv.setVisibility(View.INVISIBLE);
to
lstv.setVisibility(View.GONE);
Hope this help
You can hide the listview in the layout(xml) itself. Like this
<ListView
....
....
android:visibility="gone"
/>

Warning Unexpected text in layout file and app crashes on click

Getting Warning : unexpected text found in layout file : ""
Also on clicking the button, app is crashing.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#+id/container">
<Button android:id="#+id/bntStartService"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/broadcast_intent"
android:onClick="broadcast_intent">
</Button>
</LinearLayout>
Please help.
// try this way hope this will help you...
1. activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:id="#+id/container">
<Button android:id="#+id/bntStartService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/broadcast_intent"
android:onClick="broadcast_intent">
</Button>
</LinearLayout>
2.MainActivity .java
public class MainActivity extends Activity{
private Button bntStartService;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bntStartService = (Button) findViewById(R.id.bntStartService);
bntStartService.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
broadcast_intent(view);
}
});
}
private void broadcast_intent(View view){
Toast.makeText(this,"Button Clicked",Toast.LENGTH_SHORT).show();
}
}

One of the ImageButton not clicking & make HorizontalScrollView clickable

This seems to be a very silly problem but its just not working for me.
I have 2 ImageButtons & a HorizontalScrollView(HSV) on a page. Strangely one of the Image Button(1st one) is not clicking. Also I wanted to make the HSV clickable so I used:
android:clickable="true", which does not work
You can use the xml & activity exactly as I have posted.
This is 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:orientation="vertical" >
<RelativeLayout
android:id="#+id/topRL"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/free_msgs_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="free msgs left" />
<ImageButton
android:id="#+id/buy_imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/ic_launcher"
/>
</RelativeLayout>
<ListView
android:id="#+id/chat_page_listView"
android:layout_width="fill_parent"
android:layout_height="match_parent"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<HorizontalScrollView
android:id="#+id/chat_message_HSV"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toLeftOf="#+id/send_image_button"
android:clickable="true" />
<ImageButton
android:id="#+id/send_image_button"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</RelativeLayout>
This is the activity file:
public class MainActivity extends Activity {
ImageButton buyIB;
ImageButton sendIB;
HorizontalScrollView chatMessageHSV;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
sendIB = (ImageButton) findViewById(
R.id.send_image_button);
chatMessageHSV = (HorizontalScrollView) findViewById(
R.id.chat_message_HSV);
buyIB = (ImageButton) findViewById(R.id.buy_imageButton);
buyIB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "buy", Toast.LENGTH_SHORT)
.show();
}
});
chatMessageHSV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "hsv", Toast.LENGTH_SHORT).show();
}
});
sendIB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "send", Toast.LENGTH_SHORT).show();
}
});
}// end of onCreate
}
And I cant use button click like
public void onClick(View view)
{}
because the actual problem is containing a drawer layout where I'm using fragments & there this method doesn't work.
The problem in the ImageButton doesn't respond for you click because the ListView will respond first for the click because it above the ImageButton try to re order it , when i remove it the ImageButton respond for the click
about the HorizontallScrollView i found the solution to use the Touch Event instead of Click try this code
chatMessageHSV.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Toast.makeText(ImageButtonActivity.this, "hsv",
Toast.LENGTH_SHORT).show();
}else if (event.getAction() == MotionEvent.ACTION_UP){
// here code if the touch up
}
return false;
}
});
feed me back
The ImageButton not clicking problem can be solved by adding to ListView these 2 lines:
android:layout_below="#+id/topRL"
android:layout_above="#+id/bottomRL"

Sliding Drawer, Handle position and width of the drawer.

I recently stumbled upon the sliding drawer in Android, after creating my first project which implements the same I have a few issues. I am using a list view inside of the sliding drawer and for that I need to implement onClickListener and extend Activity, now everything is working fine, albeit the sliding drawer opens and occupies almost all of the space in the view, I want it to open only half screen. I went through various posts on internet which ask me to extend SlidingDrawer and use method onMeasure which is not feasible for me (Or is it?). Secondly I would want to place the handle button on the top not centre. Below is my code:
activity_main.xml~~~>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="top"
>
<TextView
android:text=""
android:gravity="center|center_vertical"
android:textColor="#000000"
android:textSize="20dp"
android:textStyle="bold"
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<SlidingDrawer
android:layout_width="wrap_content"
android:id="#+id/SlidingDrawer"
android:handle="#+id/slideButton"
android:content="#+id/contentLayout"
android:animateOnClick="true"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/slideButton"
>
</Button>
<LinearLayout
android:layout_width="wrap_content"
android:id="#+id/contentLayout"
android:orientation="vertical"
android:gravity="center"
android:padding="10dip"
android:background="#0080FF"
android:layout_height="wrap_content">
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
</SlidingDrawer>
</RelativeLayout>
.java file~~~~>
public class slidingDrawerDemo extends ListActivity implements OnClickListener {
private ArrayAdapter<String> listAdapter ;
Button slideButton,b1, b2,b3,b4;
SlidingDrawer slidingDrawer;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
slideButton = (Button) findViewById(R.id.slideButton);
slidingDrawer = (SlidingDrawer) findViewById(R.id.SlidingDrawer);
ListView LV = getListView(); //(ListView)findViewById(R.id.list);
String []Test ={"Residential","Commercial","Customised Projects","SEZ","Brochure"};
ArrayList<String> Plist = new ArrayList<String>();
Plist.addAll(Arrays.asList(Test));
listAdapter = new ArrayAdapter<String>(this, R.layout.list_dem, Plist);
LV.setAdapter( listAdapter );
LV.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView LV, View view,int position, long id) {
Toast.makeText(slidingDrawerDemo.this,""+position, Toast.LENGTH_SHORT).show();
if(position==0){
initOne();
}else if (position==1){
initTwo();
}else if (position==2){
initThree();
}else if (position==3){
initFour();
}else if (position==4){
initFive();
}
}
});
slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
#Override
public void onDrawerOpened() {
// slideButton.setBackgroundResource(R.drawable.closearrow);
}
});
slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {
#Override
public void onDrawerClosed() {
// slideButton.setBackgroundResource(R.drawable.openarrow);
}
});
}
My list_dem.xml file~~~>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
I found the answer:
in my Slider defination in xml:
android:topOffset="100.5dp"

relativelayout makes my button stop working

when i use linearlayout everything works like its supposed except my button is at the top and not the bottom of the screen.
when i use relativelayout my button is on the bottom like its supposed to but only works for the first listview entry after that it stops working...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button android:id="#+id/plus"
android:background="#drawable/selector"
android:layout_width="fill_parent"
android:layout_height="75dp"
android:layout_alignParentBottom="true"
/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</ListView>
<TextView android:id="#android:id/empty" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="#string/helptxt1"
/>
</RelativeLayout>
..
public void addListenerOnButton() {
final Context context = this;
button1 = (Button) findViewById(R.id.plus);
button1.setOnClickListener(new OnClickListener() {
// #Override
public void onClick(View arg0) {
createNote();
// Intent intent = new Intent(context, QuoteEdit.class);
// startActivity(intent);
}
});
}
private void createNote() {
Intent i = new Intent(this, QuoteEdit.class);
startActivityForResult(i, ACTIVITY_CREATE);
}
Your buttons wont work because when you use a Relative layout all the views/buttons are placed in the same location unless you place them above or below each other. Try doing this with your layout:
<Button
android:id="#+id/plus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/plus" >
</ListView>
or you can add an onClick to your button:
<Button
android:id="#+id/plus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:onClick="myClickMethod" />
and in your code
public void myClickMethod(View view) {
createNote();
}

Categories

Resources