Android - Add TextView to custom activity to display in multiple activities - android

I tried add TextView to custom activity to display in multiple activities, but nothing is shown.
This is my custom activity code :
LinearLayout layout;
public void SetupFooter() {
layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setGravity(android.view.Gravity.BOTTOM|android.view.Gravity.CENTER_HORIZONTAL);
TextView tv = new TextView(this);
tv.setText("abc");
layout.addView(tv);
}
This is my main_activity xml code that I want to add the footer to :
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageButton
android:id="#+id/btnGoToTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:layout_margin="5dip"
android:background="#drawable/buttongototop"
android:padding="5dip"
android:visibility="gone" />
</FrameLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:text="TextView" />
</LinearLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/white"
android:choiceMode="singleChoice"
android:headerDividersEnabled="false" />
<ListView
android:id="#+id/right_drawer"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#color/white"
android:choiceMode="singleChoice"
android:headerDividersEnabled="false" />
</android.support.v4.widget.DrawerLayout>
When I call SetupFooter() in extended activities, nothing is shown.
Are there any problems with my code ?
Sorry for my bad English and thanks for helping me!
Edited :
I added this : addContentView(layout, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
It's shown now but it's not placed at the bottom, how could I make it stay at the bottom ?

You're creating 2 views in your function, a text view and a linear layout. You're adding the text view to the linear layout, but you aren't putting the linear layout anywhere, so it isn't appearing. If you want to add it to the linear layout already in your xml, you need to give it an id so you can get a reference to it with findViewById, then add the text view to that.

Related

How can I add programmatically TextView in included layout?

I have a BaseLayout, the BaseLayout is a ParentLayout for other layouts. In other words, the other Layouts inherit/include it. The BaseLayout has DrawerLayout (shared for all layouts) but each Activity has a different content.
My BaseLayout looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<android.support.v4.widget.DrawerLayout
android:id="#+id/myDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Main Content-->
<LinearLayout
android:id="#+id/rootLinearLayout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<!--Content-->
<!--must add Other Layout elements there
must add Other Layout elements there
must add Other Layout elements there
must add Other Layout elements there
must add Other Layout elements there !-->
</LinearLayout>
<!-- Left Navigation Derawer -->
<LinearLayout
android:id="#+id/llLeftDrawer"
android:orientation="vertical"
android:layout_width="240dp"
android:layout_height="match_parent"
android:background="?attr/colorAccent"
android:padding="15dp"
android:layout_gravity="right">
<!-- Left Navigation Derawer Content-->
<TextView
android:text="Categories"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView1"
android:gravity="center"
android:textColor="#FFFFFF"
fontPath="fonts/MobileFont.ttf" />
<ListView
android:id="#+id/lvLeftMenu"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
and included in Postlayout like this:
<?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">
<include
layout="#layout/BaseLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
How can I add PostLayout elements into the BaseLayout LinearLayout id rootLinearLayout?
postLayout element is this:
<LinearLayout
android:orientation="vertical"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout1">
<TextView
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtPostTitle"
android:gravity="right" />
<TextView
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtPostDate"
android:gravity="right" />
<android.webkit.WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/wbvPostContent" />
</LinearLayout>
First of all, provide id attribute to the included Baselayout in the Postlayout.
Then, you can get the View object from the included view by calling the following from the activity that has the Postlayout :-
View includedView = (View)findViewById(R.id.idThatYouGaveToIncludedLayout);
Then, you can access all Baselayout views from the includedView object and perform the operations that you require.
For example,
LinearLayout rootLinearLayout = (LinearLayout)includedView.findViewById(R.id.rootLinearLayout);
Similar to the above code, you can obtain objects of the other views of your Baselayout.
Bruno Bieri get solution is java and i converted to xamarin:
LayoutInflater inflater = Application.Context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
View v = inflater.Inflate(Resource.Layout.your_layout, null);
// fill in any details dynamically here
TextView textView = v.FindViewById<TextView>(Resource.Id.a_text_view);
textView.Text="your text";
// insert into main view
ViewGroup insertPoint = FindViewById<ViewGroup>(Resource.id.insert_point);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
and Rohan Stark solution good for Access to elements of Included Layout
tanQ Bruno Bieri and Rohan Stark,
when you add view as include, can use all view like own main layout views. find the layout as a view and add view to it. like below:
LinearLayout linearLayout1 = (LinearLayout)findViewById(R.id.linearLayout1);
linearLayout1.addView(new TextView(context));

ListView contents in Android navigation drawer not lined up

I followed up the android docs to build a navigation drawer on my app main activity. But the contents are in a string array in my main activity java code. The listview elements are not lined up.
How do I make them lined up to the left so they all are aligned equal distance from the left side of the screen? I also want this behavior in portrait and layout modes.
This is my xml file.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="fill_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="#+id/adla"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/tvan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#color/blue"
android:textSize="#dimen/abc_action_bar_default_height_material"
android:text="#string/app_name" />
<AutoCompleteTextView
android:id="#+id/acgetd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center_horizontal" />
<requestFocus />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="bottom" >
<Button
android:id="#+id/bgetd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/skimmy"
android:text="#string/bgetd" />
<Button
android:id="#+id/brejec"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/skimmy"
android:text="#string/reject" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
<ListView
android:id="#+id/MList"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111">
</ListView>
</android.support.v4.widget.DrawerLayout>
From the link you shared, there is a layout R.layout.drawer_list_item. there is an XML layout called drawer_list_item.xml. this is the file responsible for all the item in the listView.
check for gravity in the XML and remove it or set the property to alignParentLeft
You should NOT be creating this yourself, and instead should be using the AppCompat provided NavigationLayout for this.
This is designed to set this all up for you using just menu.xml files, and using this control will ensure your drawer matches the guidelines exactly.
http://developer.android.com/reference/android/support/design/widget/NavigationView.html
I think your Text of ListView is align to the center,
android:layout_gravity="center
change that to right or left according to your requirement
which may help
I suspect that you are using android:gravity="center" instead of android:gravity="center_vertical" in the TextView for the List.

Android XML: Showing listview above viewpager

i want to show ListView above ViewPager using following code but it shows ViewPager without ListView above it. I used following XML code from the link
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ListView
android:id="#+id/wpedenyo_searched_friends_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:scaleType="fitCenter"
android:listSelector="#drawable/list_row_selector" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top" >
</android.support.v4.view.ViewPager></FrameLayout>
UPDATE
It showing following UI
Here jassmin is the content of ListView. It's showing bellow Tab but i want it above all.
FrameLayouts are meant to hold a single child.
Try LinearLayout or RelativeLayout as the parent instead.
For example, you could make a vertically oriented LinearLayout parent and have the vertical space split evenly between your ListView and ViewPager like so:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="#+id/wpedenyo_searched_friends_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:divider="#color/list_divider"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:dividerHeight="1dp"
android:scaleType="fitCenter"
android:listSelector="#drawable/list_row_selector"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
If you want one or the other child to take up more vertical space, adjust the layout_weights accordingly.
I recently required something like this, Try using AutoCompleteTextView
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.wpedenyo_searched_friends_list);
textView.setAdapter(adapter);

Place LinearLayout below FrameLayout with ListView

I want to place a LinearLayout below a FrameLayout which contains a ListView, but the content of the LinearLayout is not shown.
I also tried this out with a surrounding LinearLayout instead of the RelativeLayout without success.
The FrameLayout cannot be changed to different layout type!
The LinearLayout below will be shown and hidden programmatically.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/framelayout_container" >
<ListView
android:id="#+id/content_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#FFFFFF"
android:dividerHeight="1dp"
android:padding="0dp" >
</ListView>
<LinearLayout
android:id="#+id/description_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white" >
<include
android:id="#+id/description_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/included_layout" />
</LinearLayout>
</FrameLayout>
<LinearLayout
android:id="#+id/below_container"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_alignParentBottom="true"
android:background="#33b5e5" >
<include
android:id="#+id/below_layout"
layout="#layout/below_layout"
android:visibility="visible" />
</LinearLayout>
</RelativeLayout>
Any ideas how to get this work?
The problem is your FrameLayout is set to match the parent width and height, so it will leave no room for the LinearLayout beneath it. You would need to revise it to use its layout_height set to wrap_content, and change the inner ListView to do the same.

Top view in RelativeLayout ignoring clicks

I have a ListView inside a RelativeLayout and a small LinearLayouot with an EditText that should hover above the List. However when I click the EditText it registers a click on the ListView underneath. It seems it's a focus problem.
Here is code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/llSearchPlaces"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#dedede"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true" >
<EditText
android:id="#+id/etSearchPlaces"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:hint="Search for Places"
android:inputType="textCapWords" />
</LinearLayout>
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="#drawable/nav_selector" >
</ListView>
</RelativeLayout>
It want focus to be on the id llSearchPlaces. But the two focus attributes I set do not work.
Try reordering the views in your XML layout. Ignoring the other views/viewgroups:
<RelativeLayout ...>
<ListView .../>
<EditText .../>
</RelativeLayout>
The reason is ViewGroups tend to draw their children in the order described and pass touch events down in the opposite order, so Views that are drawn on top have a chance to act on touches first. If you order them in the XML as I describe, EditText draws later (on top of) ListView and will receive touch events before ListView does.
Try this one:
First: Create layout xml for listview say listview.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="#drawable/nav_selector"
android:paddingBottom="50dp" >
</ListView>
</LinearLayout>
Second: Create layout xml for edit text say edittext.xml
<?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="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/etSearchPlaces"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:hint="Search for Places"
android:inputType="textCapWords" />
<requestFocus />
</LinearLayout>
Third: Merge these two layouts in layout say mainlayout.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include
android:layout_alignParentTop="true"
android:id="#+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/listview"/>
<include
android:layout_alignTop="#+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/edittext"/>
</RelativeLayout>
Note: Replace ids and other attributes with your ones....
On second look, I don't see why you need a RelativeLayout at all. The effect you are achieving is a fixed EditText below your ListView. In actuality, the ListView and the EditText overlap, and you are working around this by giving the ListView padding on the bottom equal to the height of the EditText container.
A better choice would be to use a vertical LinearLayout to contain the ListView and the EditText container beneath it. Here the ListView will take up all the space available that is not used by the EditText container.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
... />
<LinearLayout
android:id="#+id/llSearchPlaces"
android:layout_width="match_parent"
android:layout_height="..."
android:orientation="horizontal"
... >
</LinearLayout>
</LinearLayout>

Categories

Resources