How do I create a sliding drawer in code? - android

I'm trying to create a sliding drawer in code, but I don't understand what to do for the AttributeSet part of the constructor.
What do I need to do for that?
Also, how do I define in code where the slider is going to show up?
Thanks,

SlidingDrawer can't new in Java code because it must define handle and content but you can inflate in XML layout as follows:
sliding_drawer.xml:
<?xml version="1.0" encoding="utf-8"?>
<SlidingDrawer
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:handle="#+id/handle"
android:content="#+id/content">
<ImageView
android:id="#id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/tray_handle_bookmark"
/>
<LinearLayout
android:id="#id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FF000000"
/>
</SlidingDrawer>
In Java code:
// you main Layout
LinearLayout mainLayout = new LinearLayout(this);
mainLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
mainLayout.setOrientation(LinearLayout.VERTICAL);
// add sliding Drawer
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
slidingDrawer = (SlidingDrawer)inflater.inflate(R.layout.sliding_drawer, mainLayout, false);
mainLayout.addView(slidingDrawer);
// get Layout for place your content in sliding drawer
LinearLayout slideContent = (LinearLayout)slidingDrawer.findViewById(R.id.content);
slideContent.addView(.....); // add your view to slideDrawer

It looks like SlidingDrawer cannot be created directly in Java code. You will need to define it in an XML layout and inflate that layout.
Sorry!

Related

Elements inside RelativeLayout not showing up

I am trying to design a 3D page curl.
In my mainActivity, I have a viewPager which contains pages to be curled. For each page I have a separate layout file.
In the layout file if i just add a text view like below it is showing up fine:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/PageTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/green"
android:text="VIEW 1" /> -->
But if I wrap the textview inside relative layout, it just shows a white blank screen and textview is not shown.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DemoActivity" >
<TextView
style="#style/PageTitle"
android:id="#+id/sampletextview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="VIEW 1" />
</RelativeLayout>
Can someone please help me to know why i am not able to use relative layout in my view layout file:
I inflate the view like this:
LayoutInflater inflater = (LayoutInflater) myAppContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view1 = inflater.inflate(mViewIds[0], null);
Try changing the second parameter of the inflate() call from null to the ViewGroup that view1 is added to. Or post the code where you add it (and include the part where you define the LayoutParams).

Fragment content position in Android

I was trying to fix this for a while and couldn't find the problem. I have a fragment in which I have ProgressBar that is centered. Here is the xml file content of fragment.
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<LinearLayout
android:id="#+id/details_form"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="false"
android:orientation="vertical"
android:visibility="gone" >
...
</LinearLayout>
</merge>
I also have activity and I just add this fragment to the activity. Here is the xml of activity
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/details_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
tools:context=".MyActivity"
tools:ignore="MergeRootFrame" />
The problem is when I navigate to this activity inside my application the progress is shown on the left side, it is not centered. I am not sure why is this happening, so I thought someone will see something I am missing. Thanks.
EDIT:
In onCreate event of my activity code (MyActivity.java) I have this code:
DetailsFragment detailsFragment = new DetailsFragment();
agreementDetailsFragment.setArguments(arguments);
getSupportFragmentManager()
.beginTransaction()
.add(R.id.details_container,
detailsFragment).commit();
and in my fragment .java file in onCreateView I have this code
View rootView = null;
/*
* Get root view
*/
LinearLayout wrapper = new LinearLayout(getActivity());
inflater.inflate(R.layout.details_fragment, wrapper, true);
rootView = wrapper;
The problem is when I navigate to this activity inside my application
the progress is shown on the left side, it is not centered. I am not
sure why is this happening, so I thought someone will see something I
am missing. Thanks
The layout with the merge tag will have as a parent a LinearLayout. In a LinearLayout the android:layout_gravity will not work and you'll need a more permissive layout like a RelativeLayout or a FrameLayout(I'm assuming that the ProgressBar and the details_form LinearLayout will replace each other using the visibility property):
RelativeLayout wrapper = new RelativeLayout(getActivity());
inflater.inflate(R.layout.details_fragment, wrapper, true);

Android: addview()- adding a new view on top of activity

I have the following layout with an imageview and textfield,
<?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"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="26dp"
android:layout_marginTop="22dp"
android:src="#drawable/a01" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/imageView1"
android:layout_marginTop="31dp"
/>
</RelativeLayout>
This layout will be transparent and I want to call this layout on top of a particular activity ,when the particular activity first starts, how to implement it with addview()?
When you want to show it:
FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content);
View.inflate(this, R.layout.overlay_layout, rootLayout);
Then when you want to remove it:
FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content);
rootLayout.removeViewAt(rootLayout.getChildCount()-1);
That's a concise solution, you should remove the View by giving the RelativeLayout an id in the XML file, then remove by: rootLayout.removeView(findViewById(R.id.the_id_of_the_relative_layout));.
Please use the following code to add the view.
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.your_layout, null);
// fill in any details dynamically here
TextView textView = (TextView) v.findViewById(R.id.a_text_view);
textView.setText("your text");
// insert into main view
View insertPoint =(View) findViewById(R.id.insert_point); // edited.
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
The layout of your calling activity should be inside FrameLayout (as in this layout last view added will be always on top of previous view ) ,
in onCreate method of calling activity inflate the given layout using LayoutInflater and use addView method of activity directly.

service show view getLayoutInflater

I would like to show two views in a service context. One is a TextView, and the other is a class that I have developed extending the basic View class.
prompt.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mtPrompt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_dark"
android:text="#string/demoString"
android:textColor="#android:color/white"
android:minLines="2"
android:textSize="25dp" >
</TextView>
and input.xml:
<com.xxx.ime.sssView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/inputView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/translucent_gray"
android:cacheColorHint="#null" >
</com.xxx.ime.sssView>
in my onInitializeInterface() implementation I inflate them as follows:
sssView sv =
(sssView) getLayoutInflater().inflate(R.layout.input, null);
TextView tv =
(TextView) getLayoutInflater().inflate(R.layout.prompt, null);
I see the sssView, but not the TextView. Any ideas why?
This isn't quite an answer, but it is a solution. Rather than having two separate views, all I needed to do was inherit my sssView from TextView rather than view. Then I inflated the one view, and placed my text where I want it using gravity.

Android: How to dynamically include a XML Layout?

I want to decompose my UI into several XML Layouts. The first one would be the main layout, and the other ones would be the content layouts.
I would like to be able to set which content_layout should be included dynamically at run-time, so I don't want to set a "layout="#+layout/content_layout" in my XML file.
Here are my layouts:
main_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="600dp"
android:layout_height="800dp" >
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<include /> <!-- I WANT TO INCLUDE MY CONTENT HERE -->
<Button
android:id="#+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Cancel" />
</RelativeLayout>
content_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/whatever"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
content_layout2.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/whatever2"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
How can I do that?
Thanks!
<RelativeLayout android:id="#+id/rl" ...
In your code:
// get your outer relative layout
RelativeLayout rl = (RelativeLayout) findById(R.id.rl);
// inflate content layout and add it to the relative layout as second child
// add as second child, therefore pass index 1 (0,1,...)
LayoutInflater layoutInflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rl.addView(1, layoutInflater.inflate(R.layout.content_layout, this, false) );
You could try using a ViewStub and just change which layout it is going to inflate programatically.
This answer discusses using one ViewStub.

Categories

Resources