Im trying to change the icon on the handle when i try to open the slidingdrawer.
When I extract the handle from the view and sets the onclicklistener or ontouchlistener it seems to never get trigget.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SlidingDrawer drawer = (SlidingDrawer)findViewById(R.id.drawer_landscape);
ImageView handle = (ImageView)findViewById(R.id.handle_l);
handle.setFocusable(true);
handle.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("test","onClick");
}
});
}
And my XML
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/base_frame_layout"
android:layout_gravity="bottom"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
</LinearLayout>
<SlidingDrawer
android:id="#+id/drawer_landscape"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:handle="#+id/handle_l"
android:content="#+id/content"
android:orientation="vertical"
android:visibility="visible"
>
<ImageView
android:id="#+id/handle_l"
android:layout_height="50px"
android:layout_width="64px"
android:padding="0px"
android:src="#drawable/icon"
/>
<TextView
android:id="#+id/content"
android:text="content"
android:layout_height="50px"
android:layout_width="64px"
/>
</SlidingDrawer>
</FrameLayout>
I was hoping this code should log "onClick" message, but it doesn't.
Any ideas?
I found it, it was pritty easy... I just need to setOnDrawerScrollListener on my drawer.
than implement the 2 methods onScrollEnded and onScrollStarted.
mSlidingDrawer.setOnDrawerScrollListener(new OnDrawerScrollListener(){
#Override
public void onScrollEnded() {
// TODO Auto-generated method stub
Log.i("ddd","onScrollEnded slider");
slider_animation.reverseTransition(HANDLE_ANIMATION_SPEED);
}
#Override
public void onScrollStarted() {
// TODO Auto-generated method stub
Log.i("ddd","onScrollStarted slider");
slider_animation.startTransition(HANDLE_ANIMATION_SPEED);
}
});
I rather use an animation on the handle instead of changing the Image bacause of the flickering.
I found it for along time , but now it is completed about it .
It is the best way for Change Image Handle when you click to view SlidingDrawer.
It have 2 Method that we use in code below.
final SlidingDrawer drawer = (SlidingDrawer)findViewById(R.id.slidingDrawer);
drawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
#Override
public void onDrawerOpened() {
arr.setImageResource(R.drawable.arrowdown);
}
});
drawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
#Override
public void onDrawerClosed() {
arr.setImageResource(R.drawable.arrowup);
}
});
Related
Hello this is my xml file
<RelativeLayout
android:id="#+id/tutorialBox"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="15dip"
android:paddingBottom="15dip">
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
</RelativeLayout>
i have made an on click listener for it
final Button closeBt = (Button) findViewById(R.id.closeBen);
closeBt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
closeBt.setText("Im a button");
}
});
for some reason when i click this button nothing happens it doesnt look like it has been clicked.
when i took the button out of the realtive layout everything worked fine
any suggestions?
Instead of this add onClick attribute to the button tag in xml.
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:onClick = "close_clicked"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
Then in Main Activity just make a new method like this.
public void close_clicked (View v){
// Your code
}
No need to add on click listner.
Your RelativeLayout does not look good, is it your main container? Maybe try it this way
<?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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
</RelativeLayout>
And a good practice is to declare your widgets globally then instantiate them in theOnCreate
public class Foo extends AppCompatActivity {
private Button closeBenny;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
closeBenny = (Button)findViewById(R.id.closeBen);
closeBenny.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
closeBenny.setText("Im a button");
}
});
}
}
i want to have two relativelayout in first relativelayout have map and in second relativelayout i have the list..,i want on starting only layout with map will be visible on screen with a button,,when i click on button then layout with listview get open from right side with new new button on the top of it,,and prevoius button get hide.and screen get divided with two parts with different layouts..i have done some thing but from starting onward m getting half half screen.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/ListView_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1" >
<RelativeLayout
android:id="#+id/rl_ListView1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5" >
<Button
android:id="#+id/getdirection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Directions" />
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" >
</fragment>
</RelativeLayout>
<RelativeLayout
android:id="#+id/rl_ListView2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:visibility="invisible" >
<Button
android:id="#+id/hide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Directions" />
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:visibility="invisible" />
</RelativeLayout>
</LinearLayout>
MainActivity
show = (TextView)findViewById(R.id.getdirection);
show1 = (TextView)findViewById(R.id.hide);
rt = (RelativeLayout)findViewById(R.id.rl_ListView2);
show.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(rt.getVisibility()==View.INVISIBLE)
{
rt.setVisibility(View.VISIBLE);
}
show.setVisibility(View.INVISIBLE);
}
});
show1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(rt.getVisibility()==View.VISIBLE)
{
rt.setVisibility(View.INVISIBLE);
}
show1.setVisibility(View.INVISIBLE);
}
});
Try this:
You are getting layout1 in half screen from starting due to weight property. You can try not giving weight in starting and give it programatically on button click.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
button1.setVisibility(View.GONE); //hide old button
layout2.setVisibility(View.VISIBLE); //show layout2
//set Relativelayout 1 to half screen
RelativaLayout.LayoutParams params = layout1.getLayoutParams();
params.weight = 0.5;
layout1.setLayoutParams(params);
}
});
Hope this helps.
Set visibility as Gone instead of Invisible
button.setOnClickListener(this);
public void onClick(View v) {
layoutid.setVisibility(View.GONE);
}
You can try with isShown() as suggested by Amiya
<b>
button2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
if(button1.isShown()) {
// Your_Staff
}
else{
// Your_Staff
}
}
});
</b>
From my view you did not set the orientation in the LinearLayout to be vertical for the two RelativeLayouts. I also suggests you put the map and Button in a FrameLayout as well instead of RelativeLayout.
i try to close the sliding drawer when i click on the return back key but its not working
here is the code i used
#Override
public void onBackPressed() {
Log.d("onBackPressed Called", FROM_SETTINGS_KEY);
slidingDrawer.close();
}
the xml :
<SlidingDrawer
android:id="#+id/slidingDrawer1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="197dp"
android:content="#+id/content"
android:handle="#+id/handle" >
<Button
android:id="#+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Handle" />
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</SlidingDrawer>
so how can i make the return back key close this sliding drawer ?
You're overriding Activity.onKeyDown, which Activity already overrides when implementing Activity.onBackPressed, then returning false for KeyEvent.KEYCODE_BACK which indicates that you have not handled this event and it should continue to be propagated.
To fix your problem stop overriding Activity.onKeyDown. Also, change your Activity.onBackPressed to call super if the SlidingDrawer isn't opened.
private SlidingDrawer slidingDrawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
slidingDrawer = (SlidingDrawer) findViewById(R.id.slidingDrawer1);
}
#Override
public void onBackPressed() {
if (slidingDrawer.isOpened()) {
slidingDrawer.close();
} else {
super.onBackPressed();
}
}
I have a activity in that I need to change the layout.
In the first layout I have four buttons to display and in the second I need a GridView to display images.
I need to show the second layout in an AsyncTask onPostExecute method.
For now, I'm trying to set two setContentViews, but I get the following exception: ClassCastException
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_focusarea);
videoBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
new LoadFiles().execute();
}
});
animateBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
new LoadFiles().execute();
}
});
pdfBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
new LoadPDFFiles().execute();
}
});
}
And in my postExecute i try like this
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
setContentView(R.layout.gallery);
girGridView=(GridView) findViewById(R.id.gridView1_bir);
girGridView.setAdapter(new ImageAdapter(this));
girGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {
Toast.makeText(getApplicationContext(), GridViewConfig.getResim_list().get(position), Toast.LENGTH_SHORT).show();
}
});
}
});
Instead of using two layouts use a single layout as bellow
<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="fill_parent" >
<LinearLayout
android:id="#+id/MyLayoutOne"
android:layout_width="fill_parent"
android:visibility="gone"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hi This is my first layout" />
<!-- Your first layout contents add here-->
</LinearLayout>
<LinearLayout
android:id="#+id/MyLayoutTwo"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hi This is my Second layout" />
<!-- Your second layout contents add here -->
</LinearLayout>
</LinearLayout>
Add your first layout contents inside MyLayoutOne and second layout contents inside MyLayoutTwo
And use following codes inside your activity,
public class MainActivity extends Activity {
LinearLayout MyLayoutOne;
LinearLayout MyLayoutTwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyLayoutOne=(LinearLayout)findViewById(R.id.MyLayoutOne);
MyLayoutTwo=(LinearLayout)findViewById(R.id.MyLayoutTwo);
// this will make first layout visible
MyLayoutOne.setVisibility(View.VISIBLE);
// this will make second layout hidden from your layout
MyLayoutTwo.setVisibility(View.GONE);
//=========================================
//in your post create add this codes
//=========================================
// this will make first layout hidden
MyLayoutOne.setVisibility(View.GONE);
// this will make second layout visible in your layout
MyLayoutTwo.setVisibility(View.VISIBLE);
//=========================================
}
}
This is a simplest method you must study fragments for better UI management. You can use viewflipper also.
So Study Fragments and Viewflipper..
Instead of that you can have a layout that contains both a wrapper for your four buttons and other for the GridView while this last one is with visibility set to 'gone'.
When the AsycTask finished, you hide the buttons layout and show the GridView layout.
Why don't you set one content view with two layouts or two fragments? A layout can be like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/text1">
</Button>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/text2">
</Button>
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/text3">
</Button>
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/text4">
</Button>
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
</GridView>
</LinearLayout>
Use one setContentView() and define separates Linear/Relative layout one for buttons and second for gridView.And hide/show the Views according to your need.
onPostExecute of AsyncTask runs on UI thread so you need not specify runonUiThread explicitly.Instead of using 2 setcontent View it is better to have 2 views in your layout file and make it visible invisible as required.
Friend change the id of wrapper1 to child as below,
<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"
tools:context=".Focusarea" >
<LinearLayout
android:id="#+id/wrapper1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:visibility="gone" >
<GridView
android:id="#+id/gridView1_bir"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" >
</GridView>
</LinearLayout>
<RelativeLayout
android:id="#+id/wrapper2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/vid_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</LinearLayout>
and initialize your linear layouts outside oncreate as below,
LinearLayout wrapper1;
RelativeLayout wrapper2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wrapper1 = (LinearLayout)findViewById(R.id.wrapper1);
wrapper2=(RelativeLayout)findViewById(R.id.wrapper2);
// this will make first layout visible
wrapper2.setVisibility(View.VISIBLE);
// this will make second layout hidden from your layout
wrapper1.setVisibility(View.GONE);
ImageView videoBtn = (ImageView) findViewById(R.id.vid_btn);
ImageView animateBtn = (ImageView) findViewById(R.id.anit_btn);
ImageView pdfBtn = (ImageView) findViewById(R.id.pdf_btn);
videoBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
new LoadFiles().execute();
}
});
animateBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
new LoadFiles().execute();
}
});
pdfBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
new LoadPDFFiles().execute();
}
});
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting VIDEOS
pDialog.dismiss();
vid=new ArrayList<String>(new ArrayList<String>(vid));
videoUrl=parsing.parse(videoUrl);
System.out.println("VIDEO URL" +videoUrl);
runOnUiThread(new Runnable() {
public void run() {
//--here you wont need to initialize again--
// this will make first layout visible
wrapper1.setVisibility(View.VISIBLE);
// this will make second layout hidden from your layout
wrapper2.setVisibility(View.GONE);
girGridView=(GridView) findViewById(R.id.gridView1_bir);
//ListView gibi buna da adapter set ediliyor.
girGridView.setAdapter(new ImageAdapter(this));
girGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {
Toast.makeText(getApplicationContext(), GridViewConfig.getResim_list().get(position), Toast.LENGTH_SHORT).show();
}
});
}
});
I'm using a listview. Before adding the main list items to the listview, i add a header with to the listview using addheaderview(v). header have two buttons.
In logcat..it gives problem at
btnhome.setOnClickListener(new View.OnClickListener() {
and it force close.
plz help me where is error in below code.
waiting for your reply
thanx in advance
enter code here
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.games_by_category);
Global globalclass=((Global)getApplicationContext());
globalclass.setpageno(0);
lstview =(ListView)findViewById(android.R.id.list);
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.header, null);
//View v = getLayoutInflater().inflate(R.layout.header, null);
v.setMinimumHeight(10);
lstview.addHeaderView(v);
btnhome=(Button)findViewById(R.id.btnHome);
btnhome.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
btnhome.setTextColor(Color.BLACK);
Global globalclass=((Global)getApplicationContext());
Intent inte=new Intent(GamesByCatActivity.this,HotGamesActivity.class);
startActivity(inte);
}
});
Just get the Buttons from the header view and set the onClickListener.
View header;
Button headerButton = header.findViewById(R.id.btn1);
headerButton.setOnClickListener(this);
listView.addHeaderView(header);
I am not able to understand your problem. What do you want to do? If you have ListView and header then you use LinearLayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/_lv_babies" >
</ListView>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#color/blue_email"
>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit" android:textSize="15dp" android:padding="10dp"
android:layout_margin="5dp" android:textColor="#color/black"
android:id="#+id/_btn_edit"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add" android:textSize="15dp" android:padding="10dp"
android:layout_margin="5dp" android:textColor="#color/black"
android:id="#+id/_btn_add"/>
</RelativeLayout>
</LinearLayout>
This is the example you can use like this.
For your button click....if its works then tick on right click or accept my answer.
protected void setXmlComponents(){
_btn_add =(Button)findViewById(R.id._btn_add);
_btn_edit =(Button)findViewById(R.id._btn_edit);
}
protected void setListener(){
_btn_add.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
_btn_edit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}