I have a problem with SlidingDrawer. All buttons in it could not be click.
Here is my xml file for SlidingDrawer:
<?xml version="1.0" encoding="utf-8"?>
<SlidingDrawer xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="110dp"
android:content="#+id/content"
android:handle="#+id/handle">
<Button
android:id="#+id/handle"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="#string/menu" />
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_gravity="center"
>
<Button
android:id="#+id/btn_video"
style="#style/button_menu_bottom"
android:drawableTop="#drawable/mnu_video"
android:text="#string/video" />
...
...
</LinearLayout>
</SlidingDrawer>
Here is my java file extended from SlidingDrawer
package com.example;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SlidingDrawer;
import com.example.R;
public class BottomMenuBar extends SlidingDrawer {
private SlidingDrawer mSlidingDrawer;
private Button mButtonSlidingDrawer;
private LinearLayout mLayoutContent;
private Button mButtonVideo;
private Button mButtonPhoto;
private Button mButtonChat;
private Button mButtonSetting;
public BottomMenuBar2(Context context, AttributeSet attrs) {
super(context, attrs);
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li;
li = (LayoutInflater) getContext().getSystemService(infService);
li.inflate(R.layout.bar_bottom_menu, this, true);
mSlidingDrawer=(SlidingDrawer)findViewById(R.id.sld_bottom_menu);
mButtonSlidingDrawer=(Button)findViewById(R.id.handle);
mLayoutContent=(LinearLayout)findViewById(R.id.content);
mButtonVideo=(Button)findViewById(R.id.btn_video);
mButtonSetting=(Button)findViewById(R.id.btn_setting);
}
public Button getmButtonSlidingDrawer() {
return mButtonSlidingDrawer;
}
public void setmButtonSlidingDrawer(Button mButtonSlidingDrawer) {
this.mButtonSlidingDrawer = mButtonSlidingDrawer;
}
...
Setter & Getter methods
...
}
Here is my main.xml file:
<?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="match_parent"
android:background="#drawable/bg_main"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1" >
...
Any content
...
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.example.BottomMenuBar
android:id="#+id/sld_bottom_menu"
android:layout_width="wrap_content"
android:layout_height="110dp"
android:handle="#+id/handle"
android:content="#+id/content"
/>
</LinearLayout>
</LinearLayout>
For my buttons in sliding drawers, I tell each button what method to use in the xml with android:onClick.
<Button
android:id="#+id/sortbutton"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:onClick="SortClickHandler"
android:text="#string/sd_sortmethod_button"
android:textSize="12dp" />
And then define the method in whatever activity is appropriate:
public void SortClickHandler(View v) {
// Do stuff here
}
EDIT
I think I found your issue. From the developer docs here it says, and I quote:
SlidingDrawer should be used as an overlay inside layouts. This means SlidingDrawer
should only be used inside of a FrameLayout or a RelativeLayout for instance.
So, I think if you change your layout to a RelativeLayout you will find that your buttons will work. I just double-checked all of my uses of a sliding drawer and in all cases the root element of the view containing it is a RelativeLayout.
I don't understand why you're extending SlidingDrawer.
You should look up the documentation here.
<SlidingDrawer
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:handle="#+id/handle"
android:content="#+id/content">
<ImageView
android:id="#id/handle"
android:layout_width="88dip"
android:layout_height="44dip" />
<FrameView
android:id="#id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
[INSERT VIEWS HERE]
<FrameView>
</SlidingDrawer>
In your Activity you don't even technically need to gain access to the SlidingDrawer object (The sliding functionality is already in place as standard, it simply contains the the view's in content, you don't need to interfere).
From your Activity gain reference to the view's you add in [INSERT VIEWS HERE]. You can then do setOnClickListener() on each of these buttons.
Related
I want to write a class which dynamically add the views like button or TextView or ...
I did it but there is some problem
1- I want to use an option that means I want to use scroll or I want to show all of the views in the screen.
1-1 I want to use scroll (Vertical or Horizontal) to show all of the views.
1-2 I want to use android:layout_weight to fit the views in the same size which are beautiful on screen on any size of screen but I don't know how.
m=number of rows
n=number of columns
2- I don't know is it a good method for describing a class for considering the Object Oriented Concepts
In XML file there is only a RelativeLayout.
the Class:
import android.app.ActionBar;
import android.content.Context;
import android.text.Layout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import java.util.ArrayList;
public class MakeTwoDimensionArrayView {
private ScrollView scrollView;
private LinearLayout mainLinearLayout;
private LinearLayout [] subLinearLayout;
MakeTwoDimensionArrayView(Context c, int m, int n, ArrayList<?> V){
scrollView=new ScrollView(c);
mainLinearLayout=new LinearLayout(c);
mainLinearLayout.setOrientation(LinearLayout.VERTICAL);
mainLinearLayout.setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
subLinearLayout=new LinearLayout[m];
scrollView.addView(mainLinearLayout);
for(int i=0; i<m; i++){
subLinearLayout[i]=new LinearLayout(c);
subLinearLayout[i].setOrientation(LinearLayout.HORIZONTAL);
subLinearLayout[i].setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
for(int j=0; j<n; j++){
int t=i*n+j;
View v= (View) V.get(t);
v.setTag(t+"");
subLinearLayout[i].addView(v);
}
mainLinearLayout.addView(subLinearLayout[i]);
}
}
public ScrollView getLayout(){
return scrollView;
}
}
The main
public class MainActivity extends AppCompatActivity {
MakeTwoDimensionArrayView makeButton;
ArrayList <Button> buttons;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int m=3, n=2;
buttons=new ArrayList<Button>();
for (int i=0; i<m*n; i++){
Button b=new Button(this);
buttons.add(b);
}
makeButton=new MakeTwoDimensionArrayView(this, m, n, buttons);
setContentView(makeButton.getLayout());
}
}
You should implement horizontalScrollView within scrollView. and in that horizontalScrollView add buttons. then it will be both vertically and horizontally scrollable. But for that you should know about ScrollView and HorizontalScrollView. because they have only one direct child layout. This is an XML file.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="55dip"
android:orientation="vertical" >
<TextView
android:id="#+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="5dp"
android:textSize="17dip" />
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/itemContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/item1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableBottom="#drawable/ic_launcher"
android:text="Item1" />
<Button
android:id="#+id/item2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableBottom="#drawable/ic_launcher"
android:text="Item2" />
<Button
android:id="#+id/item3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableBottom="#drawable/ic_launcher"
android:text="Item3" />
<Button
android:id="#+id/item3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableBottom="#drawable/ic_launcher"
android:text="Item3" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
This is only demo for you to understand how scrollView and horizontalScrollView works in an xml if you want to implement dynamically then change in your MakeTwoDimensionArrayView class and in onCreate() of activity class. if still you have question then let me know.
The PreferenceScreen isn't good enough for me, since I've to add items to a Spinner. Those items need to come from a data list.
I've got a custom ArrayAdapter that returns the name of the item, and when I click it. It returns the data that is contained within the item.
I want to use that same ArrayAdapter in a ListPreference (that's the spinner in the PreferenceScreen) but the ListPreference doesn't allow me to use a Adapter.
So, I want to recreate the look of the PreferenceScreen (with the PreferenceCategory's) without the use of the actual PreferenceScreen (and PreferenceCategory's)
Is this possible with a library? I haven't found one.
Thanks,
Tim
I tried to collect my first method - I hope I didn't forget to include some parts (aapart color definitions or statelist drawables, which is a trivial task to make your own)
Customizing the standard Preferences
/res/xml/prefs.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<!-- ... -->
<PreferenceCategory android:title="#string/pref_vibrate_cat">
<CheckBoxPreference
android:persistent="true"
android:key="vibrate"
android:title="#string/pref_vibrate_title"
android:summary="#string/pref_vibrate_summ"
android:defaultValue="true"
android:layout="#layout/prefs"
/>
</PreferenceCategory>
<!-- ... -->
<!-- Just to show how to use a custom preference (you must have the corresponding java Class in your project) -->
<PreferenceCategory android:title="#string/pref_tts_cat">
<com.dergolem.abc.CLS_Prefs_Multi
android:persistent="true"
android:key="tts"
android:title="#string/pref_tts_title"
android:summary="#string/nothing"
android:dialogTitle="#string/pref_tts_dlg"
android:dialogIcon="#android:drawable/sym_action_chat"
android:entries="#array/prefs_tts_titles"
android:entryValues="#array/prefs_tts_values"
android:defaultValue="#array/prefs_tts_defaults"
android:layout="#layout/prefs"
android:widgetLayout="#layout/arr_dn"
/>
</PreferenceCategory>
<!-- ... -->
</PreferenceScreen>
/res/layout/prefs.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for a visually child-like Preference in a PreferenceActivity. -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:baselineAligned="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingStart="16dp"
android:paddingEnd="?android:attr/scrollbarSize"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:minWidth="16dp"
android:gravity="center"
android:orientation="horizontal"
>
<ImageView
android:id="#+android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_weight="1"
>
<TextView
android:id="#+android:id/displayTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
/>
<TextView
android:id="#+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
/>
<TextView
android:id="#+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#android:id/title"
android:layout_alignStart="#android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary"
android:shadowColor="#color/white"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="1"
android:maxLines="4"
/>
</RelativeLayout>
<!-- Preference should place its actual preference widget here. -->
<LinearLayout
android:id="#+android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:minWidth="48dp"
android:gravity="center"
android:orientation="vertical"
/>
</LinearLayout>
/src/ACT_Prefs
package com.dergolem.abc;
/* ---------------------------------- Imports ------------------------------- */
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.widget.ListView;
public final class ACT_Prefs // NO_UCD (use default)
extends PreferenceActivity
implements OnSharedPreferenceChangeListener
{
/* ------------------------------ Objects ------------------------------- */
private Context ctx = null;
/* ----------------------------- Overrides ------------------------------ */
// Reload the Activity on rotation.
#Override
public final void onConfigurationChanged(final Configuration cfg)
{
super.onConfigurationChanged(cfg);
reStart();
}
/*
Load the Preference Activity if the API LEvel is less than 11 or else load
the PreferenceFragment.
Needed workaround, since unfortunately Google didn't include the
PreferenceFragment in the support library
*/
#Override
public final void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ctx = getApplicationContext();
if (Build.VERSION.SDK_INT < 11)
{
createPreference_Activity();
}
else
{
createPreference_Fragment();
}
}
#Override
protected void onPause()
{
// Unregister OnSharedPreferenceChangeListener
PreferenceManager.getDefaultSharedPreferences(ctx).
unregisterOnSharedPreferenceChangeListener(this);
// Call the base method
super.onPause();
}
#Override
protected void onResume()
{
// Register OnSharedPreferenceChangeListener
PreferenceManager.getDefaultSharedPreferences(ctx).
registerOnSharedPreferenceChangeListener(this);
// Fire the base method
super.onResume();
}
/* ------------------------------ Methods ------------------------------- */
#SuppressWarnings("deprecation")
private final void createPreference_Activity()
{
// Set the Activity layout
addPreferencesFromResource(R.xml.prefs);
// Get the PreferenceScreen ListView
final ListView lvw = getListView();
// Set the horizontal separator
lvw.setDivider(getResources().getDrawable(R.drawable.list_divider));
lvw.setDividerHeight((1));
// Set the statelist selector
lvw.setSelector(R.drawable.list_item_colors);
// Remove the top and bottom fadings
lvw.setVerticalFadingEdgeEnabled(false);
}
#SuppressLint("NewApi")
private final void createPreference_Fragment()
{
// Create the fragment.
getFragmentManager().beginTransaction().replace
(android.R.id.content, new FRG_Prefs()).commit();
getFragmentManager().executePendingTransactions();
}
}
/src/FRG_Prefs
package com.dergolem.abc;
/* ---------------------------------- Imports ------------------------------- */
import android.annotation.SuppressLint;
import android.graphics.PixelFormat;
import android.preference.PreferenceFragment;
import android.view.View;
import android.widget.ListView;
#SuppressLint("NewApi")
public final class FRG_Prefs
extends PreferenceFragment
{
/* ----------------------------- Overrides ------------------------------ */
#Override
public final void onResume()
{
super.onResume();
addPreferencesFromResource(R.xml.prefs);
init();
}
#Override
public final void onStop()
{
super.onStop();
// Kill the prefence screen, so that it won't be recreated DUPLICATE.
// HORRIBLE, but it's the only way to avoid the PreferenceScreen copycat.
getActivity().finish();
}
/* ------------------------------ Methods ------------------------------- */
private final void init()
{
final View v = getView();
v.setPadding(paddingSize, 0, paddingSize, 0);
// Get the PreferenceScreen ListView
final ListView lvw = (ListView) v.findViewById(android.R.id.list);
// Set the horizontal separator
lvw.setDivider(getResources().getDrawable(R.drawable.list_divider));
lvw.setDividerHeight((1));
// Set the state selector
lvw.setSelector(R.drawable.list_item_colors);
// Remove top and bottom fading
lvw.setVerticalFadingEdgeEnabled(false);
}
}
To show my Preferences:
startActivity(new Intent(ctx, ACT_Prefs.class));
ctx is defined as
Context ctx = getApplicationContext();
since I use it a lot, I define it once and for all.
[EDIT]
By request, I could add a method to make a Fake PreferenceScreen.
The answer above is to difficult to implement, so I've designed my own version.
The layout xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical">
<include layout="#layout/toolbar"/> <!-- This is a custom toolbar (or actionbar), and not necessary -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/toolbar"
android:paddingRight="?android:attr/scrollbarSize">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#layout/toolbar"
android:id="#+id/scrollView" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/category_battery"
android:id="#+id/category_misc"
android:layout_marginLeft="#dimen/activity_settings_header_margin" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="2dp"
android:id="#+id/divider"
android:layout_marginLeft="#dimen/activity_settings_margin"
android:layout_below="#+id/category_misc"
android:contentDescription="divider"
android:scaleType="matrix"
android:background="#android:drawable/divider_horizontal_bright"
android:src="#android:drawable/divider_horizontal_bright" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="#dimen/activity_settings_margin">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/category_calibration"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/category_subjects"
android:layout_marginLeft="#dimen/activity_settings_header_margin"
android:layout_below="#+id/batteryChargeState" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="2dp"
android:id="#+id/divider2"
android:layout_marginLeft="#dimen/activity_settings_margin"
android:layout_below="#+id/category_subjects"
android:contentDescription="divider"
android:scaleType="matrix"
android:background="#android:drawable/divider_horizontal_bright"
android:src="#android:drawable/divider_horizontal_bright" />
<LinearLayout android:layout_width="match_parent"
android:layout_below="#+id/category_subjects"
android:layout_centerVertical="true"
android:layout_height="match_parent"
android:padding="#dimen/activity_settings_margin"
android:orientation="vertical"
android:id="#+id/nextLayout">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
</RelativeLayout>
Toolbar xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
Dimens xml:
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="activity_settings_margin">24dp</dimen>
<dimen name="activity_settings_header_margin">18dp</dimen>
</resources>
Colors xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="orange">#FDA432</color>
<color name="orange_dark">#ffd17731</color>
</resources>
Just use the your way to store the Preferences. I've created a custom preference class that contains private keys so I can't post the code here without breaking it.
The advantage of using a custom layout like this is that you can add your own toolbar with this line as the first element of the first RelativeLayout.
To use the custom toolbar use this piece of code in your onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle(); // This is for the title when you use a drawer
mToolbar = (Toolbar) findViewById(R.id.toolbar); // This finds the toolbar you've specified using the <include> in the xml
setSupportActionBar(mToolbar); // This sets the toolbar to be used
mToolbar.setBackgroundColor(getResources().getColor(R.color.orange)); // This sets the color of the toolbar
if (Build.VERSION.SDK_INT >= 21) {
getWindow().setStatusBarColor(getResources().getColor(R.color.orange_dark)); // This sets the color of the navigation bar to a darker orange as used for the toolbar, only when this is supported!
}
mToolbar.setNavigationIcon(R.mipmap.ic_launcher); // This makes the icon clickable, to open and close a drawer if you have one
}
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.
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
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: