Please see the Image
hello I want to use this type of Layout in my application
If I drag one layout to any direction then i want to display other Layout
is it possible please help me
Try that
Main.java:
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerScrollListener;
public class Main extends Activity {
SlidingDrawer mSlide;
ImageView mImageSlideHandle;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSlide = ((SlidingDrawer)findViewById(R.id.slide));
mImageSlideHandle = ((ImageView)findViewById(R.id.handle));
mSlide.setOnDrawerScrollListener(new OnDrawerScrollListener(){
#Override
public void onScrollEnded() {
if (mSlide.isOpened()) {
mImageSlideHandle.setImageResource(R.drawable.ic_tray_expand);
} else {
mImageSlideHandle.setImageResource(R.drawable.ic_tray_collapse);
}
}
#Override
public void onScrollStarted() {
}
});
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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" />
<SlidingDrawer
android:layout_height="wrap_content"
android:handle="#+id/handle"
android:content="#+id/content"
android:id="#+id/slide"
android:layout_width="fill_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#id/handle"
android:src="#drawable/ic_tray_expand"
android:background="#drawable/handle" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#id/content"
android:background="#FFFFFFFF"
android:gravity="center" >
<Button
android:text="Button01"
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="Button02"
android:id="#+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</SlidingDrawer>
</LinearLayout>
you can use a SlidingDrawer ,
follow this link SlidingDrawer Tutorial
Hope it helps :)
http://techdroid.kbeanie.com/2009/08/android-sliding-drawer-example.html
http://www.androidpeople.com/android-sliding-drawer-tutorial
This is android SlidingDrawer to use this follow the above link
You could a sliding drawer or u could use the interpolator to do the same stuff
Related
This is bugging me for a while now, Im using HorizontalScrollView.fullScroll(HorizontalScrollView.FOCUS_RIGHT) which scrolls my ScrollView to the right, but not all the way, it's takes the scrollbar 99% percent to the right, I can manually scroll the rest of the way but for some reason it does not do it when i call the fullScroll() API.
Here's a sample code.... If you push the button a few times, the TextView will expand and you'll see the problem.
Activity:
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tv;
HorizontalScrollView hsv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
hsv = (HorizontalScrollView)findViewById(R.id.horizontalScrollView1);
}
public void btnOnClick(View v) {
tv.append("a");
hsv.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
}
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="push the button" />
</LinearLayout>
</HorizontalScrollView>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnOnClick"
android:text="Button" />
</LinearLayout>
I updated your code. Replace your code with below one. Which perfectly moves HorizontalScrollView to right of the screen.
Replace Your MainActivity.java with below one:
public class AndroidHTMLActivity extends Activity{
TextView tv;
HorizontalScrollView hsv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView1);
}
public void btnOnClick(View v){
tv.append("a");
System.out.println("tv.getWidth()="+tv.getWidth());
hsv = (HorizontalScrollView)findViewById(R.id.horizontalScrollView1);
ViewTreeObserver vto = hsv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
#Override
public void onGlobalLayout() {
hsv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
hsv.scrollTo(tv.getWidth(), 0);
}
});
}
}
And replace youe Xml file with below one:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/ll_parent_layout">
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="push the button" />
</LinearLayout>
</HorizontalScrollView>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnOnClick"
android:text="Button" />
</LinearLayout>
This code fulfile your condition which you wants.
I have an activity with the following layout :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/testlayoutOverlays"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/testlayoutMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/testlayout_bottom"
android:layout_width="fill_parent"
android:layout_height="62dp"
android:background="#122334" >
<ImageView
android:id="#+id/testbtnBlock"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_gravity="left|center_vertical"
android:contentDescription="Test1"
android:padding="#dimen/padding_medium"
android:src="#drawable/btnblock" />
<TextView
android:id="#+id/testtxtZoomPan"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/testbtnX"
android:layout_toRightOf="#+id/testbtnBlock"
android:gravity="center_horizontal"
android:text="#string/txtZoomPan"
android:textColor="#FFFFFF" />
<ImageView
android:id="#+id/testbtnX"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_gravity="right|center_vertical"
android:contentDescription="Test2"
android:padding="#dimen/padding_medium"
android:src="#drawable/btnx" />
</RelativeLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/testlayoutPuzzleInfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="horizontal" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/testlayoutChronoErrors"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Chronometer
android:id="#+id/testchronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:format="#string/chronometer_initial_format"
android:gravity="center" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/testlayoutErrors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:layout_width="1px"
android:layout_height="20dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
and the following code :
package minmaxdev.android.picrossanywhere;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.support.v4.app.NavUtils;
public class TestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
// Capture our button from layout
ImageView button = (ImageView) findViewById(R.id.testbtnBlock);
// Register the onClick listener with the implementation above
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
RelativeLayout rv = (RelativeLayout) findViewById(R.id.testlayoutOverlays);
rv.setGravity(Gravity.CENTER);
Button btnRetry = new Button(TestActivity.this);
btnRetry.setId(R.id.btnRetry);
btnRetry.setBackgroundResource(R.drawable.btnselector);
RelativeLayout.LayoutParams prmBtn = new RelativeLayout.LayoutParams(Util.DPsToPixels(200, getResources()), Util.DPsToPixels(40, getResources()));
prmBtn.setMargins(0, 120, 0, 0);
btnRetry.setLayoutParams(prmBtn);
// btnRetry.setGravity(Gravity.CENTER_HORIZONTAL);
btnRetry.setText("Retry");
btnRetry.setOnClickListener(new ImageView.OnClickListener() {
public void onClick(View v) {
Intent intent = getIntent();
finish();
startActivity(intent);
}
});
rv.addView(btnRetry);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_test, menu);
return true;
}
}
I would like to know :
Why is my dynamically created Button (named btnRetry) not appearing in the center of the screen, since I set the parent RelativeLayout gravity to center with rv.setGravity(Gravity.CENTER) ?
Thank you very much for your time
The answer is easy. Gravity works for the content, layout_gravity for the view that uses that.
Source basically: https://stackoverflow.com/a/3482757/180538
Try to use LayoutParams with addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
To answer your gravity understanding: You are right but I guess that this information in the documentation is important:
Note that since RelativeLayout considers the positioning of each child relative to one another to be significant, setting gravity will affect the positioning of all children as a single unit within the parent. This happens after children have been relatively positioned.
I can't test your layout at the moment but my guess is that the time where gravity is applied doesn't create the expected result.
Personally I would use gravity only in LinearLayouts and the centerInParent for RelativeLayouts.
I am using two buttons, one for next news and one for previous news. In my activity when I am on the first news my button should set the image with the grey image and when I move forward my previous news button should change from the grey image to the colored one. Similarly when I reach at the last news my next button should change to grey image.
This is my xml file.
<?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="#FFFFFFFF"
>
<TextView
android:id="#+id/tv_submitDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textColor="#000000"
android:padding="5dp"/>
<TextView
android:id="#+id/tv_headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_submitDate"
android:text="Medium Text"
android:textColor="#000000"
android:textStyle="bold"
android:padding="5dp"/>
<View
android:id="#+id/viewSeperator"
android:layout_width="fill_parent"
android:layout_height="2dip"
android:layout_below="#+id/tv_headline"
android:background="#FF909090" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/btn_relative_layout"
android:layout_below="#+id/viewSeperator" android:padding="10dp">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/tv_detailNews"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textColor="#000000" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/btn_relative_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#DCDCDC"
android:padding="10sp" >
<Button
android:id="#+id/btn_prevNews"
android:layout_width="120sp"
android:layout_height="40sp"
android:layout_weight="0.5"
android:layout_marginRight="5sp"
android:background="#drawable/arrow_left"
android:clickable="true" />
<Button
android:id="#+id/btn_nextNews"
android:layout_width="120sp"
android:layout_height="40sp"
android:layout_marginLeft="5sp"
android:layout_weight="0.5"
android:background="#drawable/arrow_right"
android:clickable="true" />
</LinearLayout>
</RelativeLayout>
This is where I am trying to change the image of my button:
public void onClick(View v) {
if (v == btnPrevNews && newsId > 0) {
if (newsId > 0) {
newsId--;
putDetailNewsinView(newsId);
} else if (newsId == 0) {
btnPrevNews
.setBackgroundResource(R.drawable.disable_arrow_left);
btnPrevNews.setEnabled(false);
}
// btnPrevNews.setVisibility(View.INVISIBLE);
} else if (v == btnNextNews && newsId < newsListDetail.size() - 1) {
if (newsId < newsListDetail.size() - 1) {
newsId++;
putDetailNewsinView(newsId);
if(newsId == 0)
btnNextNews.setBackgroundDrawable(getResources().getDrawable(
R.drawable.disable_arrow_right));
} else if (newsId == newsListDetail.size()) {
// btnNextNews.setVisibility(View.INVISIBLE);
btnNextNews.setBackgroundDrawable(getResources().getDrawable(
R.drawable.disable_arrow_right));
// btnNextNews.setBackgroundResource(R.drawable.disable_arrow_right);
btnNextNews.setEnabled(false);
}
}
}
Please help me out in this.
Thanks in advance.
I assume you have Methods which will be executed if the Button is pressed. Just use these and add Logic to change the Background of these Buttons to grey:
Give both Buttons an ID, you can access these Buttons with:
Button mybutton = (Button) findViewById(R.id.yourid);
Now you can do all kinds of things with your button, like changing the background.
edit:
The Problem could lay in the Pictures, but if i were you, i would set some breakpoints and debug step-by-step.
I hacked some code of mine to test this functionality, it works fine for me. Here is it:
package my.namespac;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SomeTestProjectActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.btn_prevNews);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button b = (Button)v;
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.koala));
}
});
}
}
And the Layout-Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btn_prevNews"
android:layout_width="120sp"
android:layout_height="40sp"
android:layout_weight="0.5"
android:layout_marginRight="5sp"
android:background="#drawable/ic_launcher"
android:clickable="true" />
</LinearLayout>
How to make a layout in android like the status bar in the home screen which allows the user to drag the status bar up to any height. The layout should also allow the user with a flick up or down to expand or contract the the layout fully.
Do you mean something like a SlidingDrawer?
Can be used like this:
/src - SliderActivity.java:
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
public class SliderActivity extends Activity {
Button slideHandleButton;
SlidingDrawer slidingDrawer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
slideHandleButton = (Button) findViewById(R.id.slideHandleButton);
slidingDrawer = (SlidingDrawer) findViewById(R.id.SlidingDrawer);
slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
#Override
public void onDrawerOpened() {
slideHandleButton.setBackgroundResource(R.drawable.arrowdown);
}
});
slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {
#Override
public void onDrawerClosed() {
slideHandleButton.setBackgroundResource(R.drawable.arrowup);
}
});
}
}
/res/layout - main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:gravity="bottom">
<SlidingDrawer android:layout_width="wrap_content"
android:id="#+id/SlidingDrawer" android:handle="#+id/slideHandleButton"
android:content="#+id/contentLayout" android:padding="10dip"
android:layout_height="200dip">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/slideHandleButton"
android:background="#drawable/arrowup"></Button>
<LinearLayout android:layout_width="wrap_content"
android:id="#+id/contentLayout" android:orientation="vertical"
android:gravity="center|top" android:padding="10dip"
android:background="#505050" android:layout_height="wrap_content">
<TextView android:id="#+id/TextView01" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_weight="8" android:text="Hello Slider"></TextView>
<Button android:id="#+id/Button02" android:layout_weight="2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Do anything"></Button>
</LinearLayout>
</SlidingDrawer>
</LinearLayout>
You need 2 images in your /res/drawable folder to make it work, like arrowup.png and arrowdown.png in this example
I want to implement the same behaviour of the notification view in the official Facebook app.
The "notifications" tab are at the bottom and can drag/drop via fingers to full screen.
How can i do that?
I've tried it via ViewFlipper and Animation.... But no success.
Does anyone know how we can do this?
The app "Zedge" has the same in the "search" function.
Via drag/drop you can open the "search" view.
Those Apps are using a SlidingDrawer. The SlidingDrawer will do all the opening and closing for you only have to define the content view and the view for the handle.
It is the same view that is also used as the application drawer on the home screen.
Additionally, here is a litte demo how the SlideingDrawer is used:
/src - SliderActivity.java:
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
public class SliderActivity extends Activity {
Button slideHandleButton;
SlidingDrawer slidingDrawer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
slideHandleButton = (Button) findViewById(R.id.slideHandleButton);
slidingDrawer = (SlidingDrawer) findViewById(R.id.SlidingDrawer);
slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
#Override
public void onDrawerOpened() {
slideHandleButton.setBackgroundResource(R.drawable.arrowdown);
}
});
slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {
#Override
public void onDrawerClosed() {
slideHandleButton.setBackgroundResource(R.drawable.arrowup);
}
});
}
}
/res/layout - main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:gravity="bottom">
<SlidingDrawer android:layout_width="wrap_content"
android:id="#+id/SlidingDrawer" android:handle="#+id/slideHandleButton"
android:content="#+id/contentLayout" android:padding="10dip"
android:layout_height="200dip">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/slideHandleButton"
android:background="#drawable/arrowup"></Button>
<LinearLayout android:layout_width="wrap_content"
android:id="#+id/contentLayout" android:orientation="vertical"
android:gravity="center|top" android:padding="10dip"
android:background="#505050" android:layout_height="wrap_content">
<TextView android:id="#+id/TextView01" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_weight="8" android:text="Hello Slider"></TextView>
<Button android:id="#+id/Button02" android:layout_weight="2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Do anything"></Button>
</LinearLayout>
</SlidingDrawer>
</LinearLayout>
Additionally you need to images in your /res/drawable folder - in this case like arrowup.png and arrowdown.png
If you put that all together it turns out to look like that: