I have the following XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rlDateOne"
android:background="#drawable/dateborder"
android:padding="#dimen/date_layout_pad" >
<ImageView
android:id="#+id/ivIcon"
android:layout_width="#dimen/cal_icon_size"
android:layout_height="#dimen/cal_icon_size"
android:src="#drawable/calicon" />
<TextView
android:id="#+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/ivIcon"
android:layout_toRightOf="#+id/ivIcon"
android:text="#string/date_one"
android:layout_marginLeft="#dimen/cal_text_left_margin"
android:textSize="#dimen/cal_text_size"
android:textColor="#color/dateholiday" />
<TextView
android:id="#+id/tvReason"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/ivIcon"
android:layout_alignLeft="#+id/tvDate"
android:text="#string/holiday_one"
android:textSize="#dimen/cal_text_size"
android:textColor="#color/dateholiday" />
</RelativeLayout>
<ImageView
android:id="#+id/ivCal"
android:layout_width="fill_parent"
android:layout_height="#dimen/divider_height"
android:src="#drawable/divider" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rlDateOne"
android:background="#drawable/dateborder"
android:padding="#dimen/date_layout_pad" >
<ImageView
android:id="#+id/ivIcon"
android:layout_width="#dimen/cal_icon_size"
android:layout_height="#dimen/cal_icon_size"
android:src="#drawable/calicon" />
<TextView
android:id="#+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/ivIcon"
android:layout_toRightOf="#+id/ivIcon"
android:text="#string/date_two"
android:layout_marginLeft="#dimen/cal_text_left_margin"
android:textSize="#dimen/cal_text_size"
android:textColor="#color/dateholiday" />
<TextView
android:id="#+id/tvReason"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/ivIcon"
android:layout_alignLeft="#+id/tvDate"
android:text="#string/holiday_two"
android:textSize="#dimen/cal_text_size"
android:textColor="#color/dateholiday" />
</RelativeLayout>
<ImageView
android:id="#+id/ivCal"
android:layout_width="fill_parent"
android:layout_height="#dimen/divider_height"
android:src="#drawable/divider" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rlDateOne"
android:background="#drawable/dateborder"
android:padding="#dimen/date_layout_pad" >
<ImageView
android:id="#+id/ivIcon"
android:layout_width="#dimen/cal_icon_size"
android:layout_height="#dimen/cal_icon_size"
android:src="#drawable/calicon" />
<TextView
android:id="#+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/ivIcon"
android:layout_toRightOf="#+id/ivIcon"
android:text="#string/date_three"
android:layout_marginLeft="#dimen/cal_text_left_margin"
android:textSize="#dimen/cal_text_size"
android:textColor="#color/dateholiday" />
<TextView
android:id="#+id/tvReason"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/ivIcon"
android:layout_alignLeft="#+id/tvDate"
android:text="#string/holiday_three"
android:textSize="#dimen/cal_text_size"
android:textColor="#color/dateholiday" />
</RelativeLayout>
</LinearLayout>
As you can see above, there is a repeat of the same code (From ImageView to </RelativeLayout> and this is just an example. I have to repeat it 41 times, instead of 3 that is shown here.
Instead of using XML how do I add it programmatically (Let's say tvDate and tvReason are String arrays)?
Is it recommended to just use XML or add it in Java?
You can make separated xml file and inflate views (any quantity) when ever you need:
...
public void onCreate(Bundle b){
super(b);
setContentView(R.layout.your_activity_layout);
...
View yourView = getLayoutInflater().inflate(R.layout.your_xml_file, null);
yourBasicLayout.addView(yourView);
...
}
Create a ListView where every item has the repeating layout. In that way you can repeat it as many times as you like.
Related
My EditText element is displaying well in popup window but its not allowing to enter values,I cant type anything inside that edittext element.
here i have attached xml code and screenshot.if you want my main activity class java code too let me know i can add it here.
xml code :
`<?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"
android:background="#android:color/background_light">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="1dp"
android:background="#android:color/darker_gray">
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="20dp">
<TextView
android:id="#+id/texttitle1"
android:textStyle="bold"
android:textSize="25dp"
android:textColor="#891800"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Create Appoinment" />
<TimePicker
android:id="#+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="spinner" />
<EditText
android:id="#+id/Easy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions"
/>
<Button
android:id="#+id/save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="openNewActivity"
android:text="Save" />
<Button
android:id="#+id/dismiss"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Back to Menu" />
</LinearLayout>
</LinearLayout>
</LinearLayout>`
take the input type text noSuggestions and replace it with text
Find the solution:
1.In Manifest file please write windowSoftInputMode :
<activity android:name=".MainActivity"
android:windowSoftInputMode="stateHidden" />
Please put these lines in main LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
3.Please check the document
https://developer.android.com/guide/topics/manifest/activity-element.html#wsoft
Try with below 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:background="#android:color/background_light"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:background="#android:color/darker_gray"
android:orientation="vertical"
android:padding="20dp">
>
<TextView
android:id="#+id/texttitle1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Create Appoinment"
android:textColor="#891800"
android:textSize="25dp"
android:textStyle="bold" />
<TimePicker
android:id="#+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="spinner" />
<EditText
android:id="#+id/Easy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions" />
<Button
android:id="#+id/save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="openNewActivity"
android:text="Save" />
<Button
android:id="#+id/dismiss"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Back to Menu" />
</LinearLayout>
</LinearLayout>
In my case, i had accidentally put android:descendantFocusability="blocksDescendants" to the parent layout,which blocks widgets inside this layout focusing. That is why, it was not working :)
If you have this line, please remove and it will work just fine
I am having a problem with the layout xml for my fragment... I think it may be because the layout was originally made for just an activity, and then later it was changed to a fragment. It looks like this in my editor:
But in my phone and emulator, it ends up like this:
Can anyone think of a solution for me? I've tried out multiple things but cannot seem to fix it, as i think maybe the rules are a little different when working with fragments? Thank you for all and any help!!
<?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">
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title" />
<EditText android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/body" />
<EditText android:id="#+id/body" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="vertical" />
<Button android:id="#+id/confirm"
android:text="#string/confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Layout file for the activity:
<?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:orientation="vertical" >
<FrameLayout
android:id="#+id/frame1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="130dp"
android:layout_toRightOf="#+id/textView1" >
</FrameLayout>
</LinearLayout>
Just change android:layout_width to match_parent. This will fill up all the available space horizontally.
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/body" />
<EditText android:id="#+id/body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="vertical" />
<Button android:id="#+id/confirm"
android:text="#string/confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
For Layout file of activity :
<FrameLayout
android:id="#+id/frame1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="130dp"
android:layout_toRightOf="#+id/textView1" >
</FrameLayout>
You can use the property "ems" to change the size.
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
for your text views and edit views try to match_parent instead of wrapping content
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title" />
<EditText android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
I have a lot of info to put on one xml page. Now i got what I want in the positions i want. However instead of fitting all my data on 3 key parts without running into each other, It keeps running into the next TextView. I was wondering how to have it auto adjust.
<?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" >
<Button
android:id="#+id/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/back" />
<TextView
android:id="#+id/makeup_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/info_title"
android:layout_marginTop="97dp"
android:text="#string/part_section_2" />
<TextView
android:id="#+id/part_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/info_title"
android:layout_marginLeft="19dp"
android:layout_marginTop="38dp"
android:text="put text here for info" />
<TextView
android:id="#+id/part_safety"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/part_makeup"
android:layout_below="#+id/safety_title"
android:layout_marginTop="28dp"
android:text="put text here for safety" />
<TextView
android:id="#+id/info_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="42dp"
android:text="#string/part_section_1" />
<TextView
android:id="#+id/part_makeup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/part_info"
android:layout_below="#+id/makeup_title"
android:layout_marginTop="33dp"
android:text="put text here for makeup" />
<TextView
android:id="#+id/safety_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/part_makeup"
android:layout_marginTop="48dp"
android:text="#string/part_section_3" />
<TextView
android:id="#+id/part_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:textColor="#b70000"
android:textSize="16sp"
android:text="put text here for part title" />
</RelativeLayout>
would ScrollView work better? and how would switch this from this to ScrollView.
skin is 480x800
density is high(240)
Strings file - http://pastie.org/8534713
Changing to ScrollView (source):
<?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="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- your Button and TextView widgets -->
</RelativeLayout>
</ScrollView>
Edit:
As well as that, try changing your back button XML to this:
<Button
android:id="#+id/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/part_safety"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="#string/back" />
The problem was android:layout_alignParentBottom="true". The bottom ended up not going to the bottom of the page like you had before. The ScrollView cut back on the bottom of the layout.
How can i place two buttons side by side with an image at the bottom of the button?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/a_button"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="Button-a" />
<Button
android:id="#+id/b_button"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="Button-b" />
</LinearLayout>
Instead of using button u can use linear-layout(Clickable) and use its id instead of Button id.something like this...... Using this you can customize your Button as per your requirements.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
//Button-A
<LinearLayout
android:id="#+id/a_button"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="0.50"
android:orientation="vertical"
android:clickable="true">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:text="Text-A"
android:paddingTop="10dip"
/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/image_A"
android:paddingTop="13dip" />
</LinearLayout>
//For Horizontal partition line
<TextView
android:layout_width="2dp"
android:layout_height="fill_parent"
android:background="#303030"
android:paddingTop="20dip" />
//Button-B
<LinearLayout
android:id="#+id/b_button"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="0.50"
android:orientation="vertical"
android:clickable="true">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Text-B"
android:gravity="center_horizontal|center_vertical"
android:paddingTop="10dip" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/image_B"
android:paddingTop="13dip"
/>
</LinearLayout>
</LinearLayout>
You can place images at the bottom of buttons with the Drawable_Bottom property.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#android:attr/buttonBarStyle"
android:orientation="horizontal" >
<Button
android:id="#+id/a_button"
style="#android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableBottom="#android:drawable/btn_star"
android:text="Button-a" />
<Button
android:id="#+id/b_button"
style="#android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableBottom="#android:drawable/btn_star"
android:text="Button-b" />
</LinearLayout>
Just add below line to both your buttons and specify the drawable you want to use.
android:drawableBottom="#drawable/image"
Hope this helps.
EDIT:
Above method works if you want to use your drawables as it is i.e. without modifying its dimensions. If you wish you modify you drawable dimentions as well then you can do that through simple Java code written below:
((Button)findViewById(R.id.button)).setCompoundDrawables(null, null, null, getDrawable(getApplicationContext(), R.drawable.image, 25));
.....
.....
Drawable getDrawable(Context context, int drawable, float form_factor) {
Drawable imgDrawable = context.getResources().getDrawable(drawable);
imgDrawable.setBounds(0, 0, (int)form_factor, (int)form_factor);
return imgDrawable;
}
I have <.ImageView>enter code heres(icons) in my android layout xml file, which are encompassed by a <ScrollView>.
Eg.
<.ScrollView><br>
<.LinearLayout><br>
<.ImageView> ...<./ImageView><br>
<./LinearLayout><br>
<./ScrollView>
To this layout, i want to attach a fixed footer. Basically a small tab present on the bottom on the screen which stays even when I scroll to the all the way down or all the way to the top.
I tend to write up layouts like this with a LinearLayout as root element and weights on the children to dynamically allocate screen estate. It keeps the layout definition compact and doesn't require defining extra ids.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView ... />
<ImageView ... />
...
<ImageView ... />
</LinearLayout>
</ScrollView>
<!-- footer here -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
...
</LinearLayout>
</LinearLayout>
Using a RelativeLayout as root element, with the ScrollView positioned 'above' the bottom-aligned footer, however, is probably slightly better in terms of performance, although I doubt it'll make a noticable different in the case of a simple view hierarchy like this. The RelativeLayout approach does require assigning some ids (to at least the footer, I'd say).
something like this..
<.ScrollView>
<.LinearLayout>
<.ImageView> ...<./ImageView>
<./LinearLayout>
<./ScrollView>
<LinearLayout>
<-- your footer here -->
</LinearLayout>
U can try like this also..
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/linearLayout1"
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:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/blackbg"
android:layout_alignParentBottom="true" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button" />
</RelativeLayout>
</RelativeLayout>