How to add row margin while inflating a LinearLayout? - android

I have a Linearlayout part of a Tablerow of tablelayout. Below is sample description
<!-- Master Layout-->
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent" android:layout_width="match_parent"
android:stretchColumns="0" android:baselineAligned="false">
<TableRow>
<Button></Button>
</Tablerow>
<TableRow>
<LinearLayout android:id="#+id/listinfo"></LinearLayout>
</Tablerow>
Then I wrote another linearlayout to create rows of linearlayout which I inflate and add to
linearlayout of Main Layout.
<!-- listrow layout-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android"
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_gravity="left"
android:src="#drawable/icon" />
<TextView android:layout_weight="1" android:id="#+id/textView1" />
<TextView android:layout_weight="1" android:id="#+id/textView1" />
</LinearLayout>
Everything is working perfectly fine but I am not getting row demarcators. How to do that?
Java code
LinearLayout placeHolderLinearLayout = (LinearLayout)findViewById(R.id.listinfo);
for (int i =0; i < myarray.size(); i++) {
final Employee eobj = myarray.get(i);
LayoutInflater vi = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout lrow = (LinearLayout)vi.inflate(R.layout.listrow, null);
lrow.setBackgroundColor(android.graphics.Color.WHITE);
lrow.setPadding(2, 2, 2, 2);
((TextView)lrow.getChildAt(1)).setText(eobj.getName());
//some more settings
placeHolderLinearLayout.addView(lrow);
}
The view doesn't show any demarcator between subsequent linearlayout. How can I achieve that?
|---------------------|
| lrow1 |
|______demarcator_____|
| lrow2 | => The demaractor is missing in my view
|_____________________|

LinearLayout lrow = (LinearLayout)vi.inflate(R.layout.listrow, null);
Try to pass placeHolderLinearLayout instead null.
If you define true as third parameter the infalted layout will be added automatically to the given ViewGroup. false will avoid that.

Related

Android Xamarin - FindViewById returns all except the top LinearLayout

Using Xamarin.Android native in Visual Studio 2017.
Here is an example of my layout. Let's call it 'headache.axml'.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/topLinearLayoutId"
android:paddingLeft="20dp"
android:paddingTop="0dp"
android:paddingRight="20dp"
android:paddingBottom="15dp"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:onClick="showPasswordDialog"
android:orientation="horizontal" >
<TextView android:id="#+id/sampleId_0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/sampleId_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
. . . more views . . .
</LinearLayout>
And here is the code where I inflate this layout.
View view = inflater.Inflate(Resource.Layout.fragment_selection, container, false);
ViewStub stub = (ViewStub) view.FindViewById(stubIds[i]);
stub.LayoutResource = Resource.Layout.headache;
View inflatedView = stub.Inflate();
Then I can use inflatedView.FindViewById() to get any view from my layout except the top LinearLayout.
For example, these lines work perfect:
var txtView0 = (TextView) inflatedView.FindViewById(Resource.Id.sampleId_0);
var txtView1 = (TextView) inflatedView.FindViewById(Resource.Id.sampleId_1);
Both txtView0 and txtView1 are initialized.
And here is my problem, when I try to get the top View from my layout I get null:
var top = (LinearLayout) inflatedView.FindViewById(Resource.Id.topLinearLayoutId); // returns null
As a workaround,
I've added a pre-top LinearLayout to my 'headache.axml'. It's become like this:
<?xml version="1.0" encoding="utf-8"?>
<!-- workaround: pre-top LinearLayout (still inaccessible) -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/glitchyLinearLayout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal">
<!-- workaround: accessible LinearLayout -->
<LinearLayout
android:id="#+id/topLinearLayoutId"
android:paddingLeft="20dp"
android:paddingTop="0dp"
android:paddingRight="20dp"
android:paddingBottom="15dp"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView android:id="#+id/sampleId_0"
... all the same ... />
<TextView android:id="#+id/sampleId_1"
... all the same ... />
. . . more views . . .
</LinearLayout>
Now I can get my topLinearLayoutId.
var top = (LinearLayout) inflatedView.FindViewById(Resource.Id.topLinearLayoutId);
In this case, the top is initialized and my application works well.
But I'd like to understand why the hell I've got this problem.

how to add my layout design in loop

I'm trying to add multiple layouts in one root layout using loop but I dont know how I can do that. My layout is code is
<FrameLayout 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"
android:id="#+id/framlayout"
tools:context="layout.SettingFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/linearLayout"
android:padding="10dp"
android:weightSum="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="5dp"
android:id="#+id/linearLayout1"
android:background="#drawable/texview_design"
android:backgroundTint="#color/popup_tv_color"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Baatein Ye Kabhi Na"
android:gravity="center"
android:textColor="#android:color/holo_red_dark"
android:textSize="20dp"
android:textStyle="italic"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hindi Romantic"
android:paddingBottom="10dp"
android:layout_gravity="center_horizontal"/>
<RatingBar
android:layout_width="144dp"
android:layout_height="wrap_content"
android:id="#+id/ratingBar1"
android:layout_gravity="right" />
</LinearLayout>
</LinearLayout>
</ScrollView>
But i want
Custom List View is best option in your case
follow this link:
http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92
https://www.caveofprogramming.com/guest-posts/custom-listview-with-imageview-and-textview-in-android.html
or
do you want include layout conformly you can by adding following code
RelativeLayout rl = (RelativeLayout) findById(R.id.rl); //id of layout which layout you want add chields
LayoutInflater layoutInflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rl.addView(1, layoutInflater.inflate(R.layout.content_layout, this, false) ); //content_layout is your layout with tw0 textview and rating bar
Hoping it will work for you
Better to use listview which is easy to implement and build for this types of task.
But you can add like below
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < 3; i++) {
View view = inflater.inflate(R.layout.yourLayoutId, null);
// Assume you want to add your view in linear layout
linearLayout.addView(view);
}

How remove scrollview programmatically (e.q. disable/enable wordwrap)?

XML (variant 1) - text WORD WRAP OFF:
<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:layout_height="wrap_content"
android:layout_width="wrap_content">
<EditText
android:inputType="textMultiLine|textNoSuggestions"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</HorizontalScrollView>
</LinearLayout>
How remove HorizontalScrollView programmaticaly, after that XML should be, like :
XML (variant 2) - TEXT WORD WRAP ON
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<EditText
android:inputType="textMultiLine|textNoSuggestions"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</LinearLayout>
What the appropriate way for do this:
Maybe use dynamic fragment ? Maybe use View.removeView ?
Or....
Please help...
This code should remove the scroll view and put the item in it's place (For this to work be sure to add the id's to the view elements on the xml).
View bodyLayout = (View)m_dialog.findViewById(R.id.my_item);
ScrollView scroller = (ScrollView)m_dialog.findViewById(R.id.my_scroll);
ViewGroup topLayout = (ViewGroup)scroller.getParent();
ViewGroup.LayoutParams params = scroller.getLayoutParams();
int index = topLayout.indexOfChild(scroller);
scroller.removeAllViews();
topLayout.removeView(scroller);
topLayout.addView(bodyLayout, index, params);

How to make template xml

I want to make XML template to reuse the code. The problem is that one of the template has several linearlayouts. However, when I run the app, it only shows one linearLayout. (I can only see id: manage_services. not manage_view neither manage_shows).
Here is my code. I hope anyone can figure out the problem.
drawable_manage.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="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/manage_services"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:drawableLeft="#drawable/menu_settings"
android:text="#string/title_manage_services"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>
<View
android:id="#+id/manage_view"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#color/menu_divider" />
<LinearLayout
android:id="#+id/manage_shows"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="#dimen/slide_menu_header" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:drawableLeft="#drawable/menu_settings"
android:text="#string/title_manage_shows"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
MainMenuActivity.java
private void addPanels(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
android.support.v4.widget.DrawerLayout parent = (android.support.v4.widget.DrawerLayout) inflater.inflate(R.layout.activity_main_menu,null);
View layout_manage = inflater.inflate(R.layout.drawable_layout_manage, null);
View layout_names = inflater.inflate(R.layout.drawable_layout_names, null);
View layout_emails = inflater.inflate(R.layout.drawable_layout_email, null);
LinearLayout leftLinearLayout = (LinearLayout)parent.findViewById(R.id.left_drawer);
leftLinearLayout.addView(layout_names);
leftLinearLayout.addView(layout_emails);
leftLinearLayout.addView(layout_manage);
setContentView(parent);
}
Change your parent LinearLayout's orientation from horizontal to vertical.
android:orientation="vertical"
Are you wanting to reuse your drawable_manage.xml linearlayouts in these (i.e. #+id/manage_services, #+id/manage_shows) or do you have separate layout files?
View layout_manage = inflater.inflate(R.layout.drawable_layout_manage, null);
View layout_names = inflater.inflate(R.layout.drawable_layout_names, null);
View layout_emails = inflater.inflate(R.layout.drawable_layout_email, null);
LinearLayout leftLinearLayout = (LinearLayout)parent.findViewById(R.id.left_drawer);
Try as others suggested and change the orientation, and make sure you are trying to inflate the correct layout. Make sure you are inflating drawable_manage in the correct location and calling to the child linearlayouts by the correct ids.

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