I am developing an android application in which I have used webview instead of Textview to use the justify property on each list item .
list_item.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:background="#color/white"
android:orientation="vertical"
android:padding="10dp" >
<WebView
android:id="#+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Question"
android:textSize="20sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#color/color_black"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:weightSum="2" >
<TextView
android:id="#+id/select_yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Yes"
android:textSize="30sp" />
<TextView
android:id="#+id/select_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="No"
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
CustomListAdapter.java
In getView()
String question = "<html><body>"
+ "<p align=\"justify\">"
+ data.get(position).getStudyCrit()
+ "</p> "
+ "</body></html>";
holder.question.loadData(question, "text/html", "utf-8");
If I run the application, the
But,After I change the scroll position and again go to previous position. the height of webview is increased like below,
Note : There is no issues with TextView, the problem arised when I replaced TextView with WebView
Please help me finding the solution.
You can use TextView instead of WebView and in TextView you can set text like:
textView.setText(Html.fromHtml(question));
And you can define maxLines for TextView.
You don't need to use webview just to justify the content. You can use the following code:
String question = "<html><body>"
+ "<p align=\"justify\">"
+ data.get(position).getStudyCrit()
+ "</p> "
+ "</body></html>";
yourtextview.setText(Html.fromHtml(question));
Related
I'm making a market application as a project for my university. As such I am loading products from my database and need to display them in a list for which I want to use a ScrollView since I am fairly certain that all of the data will not be displayable on the screen at once.
I gave the ScrollView and the included LinearLayout inside the ScrollView all tags I thought necessary to contain all of that and then started adding TextViews with the informations about the products and added those into the LinearLayout but when trying to run this on the emulator the last object at the very bottom of the LinearLayout gets cut off roughly in the middle.
I've been looking around for ways to fix this after I couldn't come up with anything but nothing worked, I've seen here the ideas of making the scrollview's gravity centered and that didn't work, center_vertical didn't either, giving it the attribute android:fillViewPort="true" did nothing either android:layout_weight="1" had no effect and android:orientation="vertical" has been in there ever since I initialized the ScrollView.
So I'm kind of baffled at this because even if i try to resize the LinearLayout via Hardcoding it to expand by 1000px every time an object is added, that doesn't do anything, in fact up until hardcoding 6000px onto the Layout it doesn't even respond in any meaningful way and at that point it just basically duplicates itself but without any content. I don't understand what is going wrong. So this I pose as a question to you.
The following contains the XML code for the entire activity layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/base_content"
tools:showIn="#layout/activity_basic_list">
<View
android:id="#+id/bar"
android:layout_width="match_parent"
android:layout_height="62dp"
android:background="#color/base_bar"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</View>
<ToggleButton
android:id="#+id/menu"
android:layout_width="62dp"
android:layout_height="62dp"
android:background="#color/base_bar"
android:contentDescription="#string/menu"
android:textOff="+"
android:textOn="-"
android:textSize="32sp"
app:layout_constraintStart_toStartOf="#id/bar"
app:layout_constraintTop_toTopOf="#id/bar" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="99dp"
android:layout_marginLeft="99dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="99dp"
android:layout_marginRight="99dp"
android:backgroundTint="#000000"
android:ems="10"
android:hint="#string/search"
android:inputType="text"
android:textColor="#000000"
android:textColorHint="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/bar" />
<ImageButton
android:id="#+id/home"
android:layout_width="62dp"
android:layout_height="62dp"
android:layout_marginStart="177dp"
android:layout_marginLeft="177dp"
android:layout_marginEnd="177dp"
android:layout_marginRight="177dp"
android:background="#color/base_bar"
android:contentDescription="#string/logo_desc"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#android:drawable/btn_dialog" />
<ScrollView
android:layout_width="409dp"
android:layout_height="465dp"
android:layout_marginTop="188dp"
android:layout_weight="2"
android:fillViewport="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" />
</ScrollView>
</android.support.constraint.ConstraintLayout>
And here is also the Code of the java segment that adds Views to the Layout:
for(int i = 0; i < jarray_stock.length(); i++){
System.out.println(i);
try {
JSONObject jobj = jarray_stock.getJSONObject(i);
TextView tv = new TextView(getApplicationContext());
// tv.setWidth(1000);
// tv.setHeight(500);
tv.setLayoutParams(new LinearLayout.LayoutParams(1000, 500));
// tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setBackgroundColor(getColor(R.color.error_content));
tv.setX(0);
tv.setY(i * (40));
tv.append("\n Name: " + jobj.getString("article_name"));
tv.append("\n Fach: " + jobj.getString("article_module"));
tv.append("\n ISBN: " + jobj.getString("ISBN"));
tv.append("\n Zustand: " + jobj.getString("status"));
tv.append("\n\n Verkäufer: " + jobj.getString("selling_user"));
tv.append("\n Preis: " + jobj.getString("price"));
tv.append("\n");
tv.getLayoutParams().height = LinearLayout.LayoutParams.WRAP_CONTENT;
Button jB = new Button(getApplicationContext());
jB.setWidth(50);
jB.setHeight(50);
jB.setX(750);
jB.setY(225+(i*(500 + 40)));
jB.setTextSize(17);
jB.setText(">>>");
jB.setTextColor(Color.WHITE);
jB.setBackgroundColor(getColor(R.color.base_active));
// parent_content.setMinimumHeight((jarray_stock.length() * 40) + 2000);
parent_content.addView(tv);
// parent_content.addView(jB);
tv.requestLayout();
} catch (JSONException e) {
e.printStackTrace();
}
}
Oh and the Button being created in there is not added, that line is commented out on purpose, I want to fix the problem with the ScrollView before fondling around with the button placement, that's also why the location assignment doesn't make any sense currently.
Thank you for your patience.
Put below code:
<LinearLayout
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
I've set my buttons to be arranged from top to bottom in order of Facebook, Instagram, Twitter and Reddit but it's not doing that, it's only showing Facebook and Reddit and I don't know why.
I've got my buttons to automatically resize on Activity creation with this code:
public void getScreenRes() {
DisplayMetrics display = this.getResources().getDisplayMetrics();
int screenwidth = display.widthPixels;
int screenheight = display.heightPixels;
double buttonheight = screenwidth / 2.66666667;
int buttonheightint= (int) Math.round(buttonheight);
ImageButton fbLogin = (ImageButton) findViewById(R.id.facebookLogin);
ViewGroup.LayoutParams fb = fbLogin.getLayoutParams();
fb.width = screenwidth;
fb.height = buttonheightint;
fbLogin.setLayoutParams(fb);
ImageButton instaLogin = (ImageButton) findViewById(R.id.instagramLogin);
ViewGroup.LayoutParams insta = instaLogin.getLayoutParams();
insta.width = screenwidth;
insta.height = buttonheightint;
instaLogin.setLayoutParams(insta);
ImageButton twitLogin = (ImageButton) findViewById(R.id.twitterLogin);
ViewGroup.LayoutParams twit = instaLogin.getLayoutParams();
twit.width = screenwidth;
twit.height = buttonheightint;
twitLogin.setLayoutParams(twit);
ImageButton redditLogin = (ImageButton) findViewById(R.id.redditLogin);
ViewGroup.LayoutParams reddit = instaLogin.getLayoutParams();
reddit.width = screenwidth;
reddit.height = buttonheightint;
redditLogin.setLayoutParams(reddit);
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="16dp"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.eren.valour.FirstTimeLogin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/getStarted"
android:id="#+id/getStartedText"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:background="#drawable/facebook"
android:id="#+id/facebookLogin"
android:onClick="facebookLogin"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/instagram"
android:layout_below="#+id/facebookLogin"
android:id="#+id/instagramLogin"
android:onClick="instagramLogin"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/twitter"
android:layout_below="#+id/instagramLogin"
android:id="#+id/twitterLogin"
android:onClick="twitterLogin"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/reddit"
android:layout_below="#+id/twitterLogin"
android:id="#+id/redditLogin"
android:onClick="redditLogin"
/>
What it looks like now:
You need to get the corresponding Layoutparams.
ViewGroup.LayoutParams reddit = instaLogin.getLayoutParams();
should be
ViewGroup.LayoutParams reddit = redditLogin.getLayoutParams();
the same with the Twitter Login:
ViewGroup.LayoutParams twit = twitLogin.getLayoutParams();
What youre doing is taking the instagram layoutparams and applying it to 3 buttons so they all get placed on top of another, so only the last shows. Probably just a copy+past error on your side, but that should fix it.
If that does not fix it, the problem should be in the layout file. Try using a LinearLayout or setting the correct attributes for your RelativeLayout.
Try to change your Layout from RelativLayout to LinearLayout. That should do it.
You can put it into a LinearLayout with a weightsum Like so. Without the java method.
<LinearLayout 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:orientation="vertical"
android:paddingTop="16dp" android:weightSum="5"
tools:context="com.eren.valour.FirstTimeLogin">
<TextView
android:id="#+id/getStartedText" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" android:layout_weight="1" android:padding="50dp" // you can tweak this figure here
android:text="getStarted\nhere" android:textAppearance="?android:attr/textAppearanceLarge"/>
<ImageButton
android:id="#+id/facebookLogin" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_weight="1"
android:background="#drawable/fb" android:onClick="facebookLogin"/>
<ImageButton
android:id="#+id/instagramLogin" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_weight="1"
android:background="#drawable/insta" android:onClick="instagramLogin"/>
<ImageButton
android:id="#+id/twitterLogin" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_weight="1"
android:background="#drawable/twit" android:onClick="twitterLogin"/>
<ImageButton
android:id="#+id/redditLogin" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_weight="1"
android:background="#drawable/reddit" android:onClick="redditLogin"/>
</LinearLayout>
Update your XMl..
Instead of RelativeLayout ,I Think you should you should use LinearLayout and Weight propert like this ...
<LineaLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="16dp"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.eren.valour.FirstTimeLogin"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/getStarted"
android:id="#+id/getStartedText"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_weight="1"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="100dp"
android:background="#drawable/facebook"
android:id="#+id/facebookLogin"
android:onClick="facebookLogin"
android:layout_weight="1"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/instagram"
android:layout_below="#+id/facebookLogin"
android:id="#+id/instagramLogin"
android:onClick="instagramLogin"
android:layout_weight="1"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/twitter"
android:layout_below="#+id/instagramLogin"
android:id="#+id/twitterLogin"
android:onClick="twitterLogin"
android:layout_weight="1"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/reddit"
android:layout_below="#+id/twitterLogin"
android:id="#+id/redditLogin"
android:onClick="redditLogin"
android:layout_weight="1"
/>
Also you don't need this method getScreenRes() anymore.
I am designing an Android XML layout that contains a few ImageViews representing images of checkmarks and red Xs. I have run into a few issues with these images containing extra margin/padding when presented to the screen.
Notice that there appears to be no Right or Left margin/padding but there is plenty of top/bottom padding.
The images are PNG formats following the guidelines for creating Small/Context Icons described at http://developer.android.com/design/style/iconography.html
I would like to correct the issue by having the margin/padding match the effect of its horizontal margin/padding.
Here is the XML of the layout:
<!-- Event Reoccuring -->
<LinearLayout
android:id="#+id/event_reoccur_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="5dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
>
<TextView
android:id="#+id/kid_event_reoccur_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_gravity="center_vertical"
android:text="#string/event_reoccur"
/>
<ImageView
android:id="#+id/kid_event_reoccur_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:layout_gravity="center_vertical"
android:contentDescription="#string/indicator"
/>
<TextView
android:id="#+id/kid_event_reoccur"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:layout_gravity="center_vertical"
android:paddingLeft="4dp"
android:text="#string/not_applicable"
/>
</LinearLayout>
<!-- Age Required -->
<LinearLayout
android:id="#+id/event_age_req_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="16dp"
android:paddingRight="16dp"
>
<TextView
android:id="#+id/kid_event_age_req_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_gravity="center_vertical"
android:text="#string/event_age_req"
/>
<ImageView
android:id="#+id/kid_event_age_req_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:gravity="center_vertical"
android:contentDescription="#string/indicator"
/>
<TextView
android:id="#+id/kid_event_age_req"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:layout_gravity="center_vertical"
android:paddingLeft="4dp"
android:text="#string/not_applicable"
/>
</LinearLayout>
Here is the code that populates the ImageViews (Note - mReoccurIndcator & mAgeReqIndicator are ImageViews while mReoccurence & mAgeReq are TextViews):
#TargetApi(16)
private void setupEventReoccurence(View v) {
// Get Reference
mReoccurence = (TextView) v.findViewById(R.id.kid_event_reoccur);
mReoccurence.setText(boolToString(mEvent.isReoccuring()));
// Checkmark/X indicator
mReoccurIndicator = (ImageView) v.findViewById(R.id.kid_event_reoccur_indicator);
mReoccurIndicator.setImageResource(R.drawable.greencheck_small_context_icon);
mReoccurIndicator.setScaleType(ScaleType.CENTER_INSIDE);
mReoccurIndicator.setCropToPadding(true);
//mReoccurIndicator.setMaxHeight((int) mReoccurence.getTextSize());
//mReoccurIndicator.setMinimumHeight((int) mReoccurence.getTextSize());
Log.d(TAG, "getTextSize() as float: " + mReoccurence.getTextSize());
Log.d(TAG, "getTextSize() as int: " + (int) mReoccurence.getTextSize());
}
#TargetApi(16)
private void setupEventAgeReq(View v) {
// Get Reference
mAgeReq = (TextView) v.findViewById(R.id.kid_event_age_req);
mAgeReq.setText(boolToString(mEvent.isAgeReq()));
// Checkmark/X indicator
mAgeReqIndicator = (ImageView) v.findViewById(R.id.kid_event_age_req_indicator);
mAgeReqIndicator.setImageResource(R.drawable.greencheck_small_context_icon);
mAgeReqIndicator.setScaleType(ScaleType.CENTER_INSIDE);
mAgeReqIndicator.setCropToPadding(true);
//mAgeReqIndicator.setMaxHeight((int) mReoccurence.getTextSize());
//mAgeReqIndicator.setMinimumHeight((int) mReoccurence.getTextSize());
Log.d(TAG, "getTextSize() as float: " + mReoccurence.getTextSize());
Log.d(TAG, "getTextSize() as int: " + (int) mReoccurence.getTextSize());
}
Just found a solution through another question:
Unwanted padding around an ImageView
android:adjustViewBounds="true" on the ImageView XML object solves issue.
Well you can also do something like below
ImageView image = new ImageView(this);
image.setAdjustViewBounds(true);
image.setImageResource(R.drawable.image_file);
Use RelativeLayout instead of LinearLayout. Also, try to avoid using attributes like weight unless it is necessary.
I have read many post similar to this, but none of them resolved my problem. Another funny thing is, on the emulator the place for the edittext is held (meaning other widgets are shifted down), but is not shown, on the other hand the same implementation on the real phone doesn't even show a place for the widget. I have the following LinearLayout inside which I need to dynamically add an EditText widget when the TextView Add a Team Member is clicked.
<LinearLayout
android:id="#+id/layout_add_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/separator"
android:layout_margin="5dp"
android:background="#color/yellow_background"
android:orientation="vertical"
android:padding="5dp">
<LinearLayout
android:id="#+id/layout_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/iv_check_research_tasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_check_empty" />
<EditText
android:id="#+id/et_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:selectAllOnFocus="true"
android:text="Task Name"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tv_add_team_member"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="5dp"
android:text="Add a team member"
android:textColor="#color/text_green"
android:textSize="15dp"
android:textStyle="bold" />
<LinearLayout
android:id="#+id/ll_add_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_add_team_member"
android:orientation="horizontal"
android:weightSum="100" >
<ImageView
android:id="#+id/iv_create_assignment_attach"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="10"
android:src="#drawable/icon_flag_alt" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:layout_weight="15"
android:text="Due"
android:textSize="15dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="65"
android:hint="None"
android:selectAllOnFocus="true" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="10"
android:src="#drawable/icon_calendar_empty" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_create_assignment_trash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:layout_marginTop="5dp"
android:text="Trash" />
<Button
android:id="#+id/btn_create_assignment_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="5dp"
android:text="Done" />
</LinearLayout>
</LinearLayout>
Here is the Java code, the onClickListener implementation of the TextView.
tvAddTeamMember.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout) getActivity().findViewById(R.id.layout_task_name);
EditText et = new EditText(getActivity());
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setText("Text");
ll.addView(et);
}
You may proceed this way ! This is not what you're looking for. But your problem will get resolved.
1) By default add the EditText view in your XML and make it invisible.
2) On Clicking the TextView, make it visible.
enjoy !
Hi #Anas Azeem: your code is correct.through your code we can add editetxt to linear layout dynamically. problem is in the layout file only.in the layout mentioned the orientation for added layout as "horizontal" ,instead of that one use "vertical" like below
<LinearLayout
android:id="#+id/layout_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/iv_check_research_tasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<EditText
android:id="#+id/et_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:selectAllOnFocus="true"
android:text="Task Name"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
Try to change the line :
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
With this:
android.widget.LinearLayout.LayoutParams p= new android.widget.LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//Then do your work
If you know you might be adding a layout, you could always declare ViewStub. After that you simply find the viewStub and call inflate() on it:
ViewStub stub = (ViewStub) findViewById(R.id.stub);
View inflated = stub.inflate();
This is the promoted way to do that in Android SDK. Thanks to that you can define the infalte layout in an xml file.
Try with this code.
replace ur line ll.addView(et);
by::
ll.addView(et,p);
and delete ur line et.setLayoutParams(p);
A simple way to do this.
Button add = (Button) findViewById(R.id.add_checkpoint);
final LinearLayout layout = (LinearLayout) findViewById(R.id.addLayout);
// layout has assigned weight = "1.0" in xml & it's a child of ScrollView
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater oLayoutInflater = getLayoutInflater();
// oView is declared globally & Layout "row_checkpoint" contains a EditText
oView = oLayoutInflater.inflate(R.layout.row_checkpoint, null);
final EditText speciesEditText = (EditText) oView
.findViewById(R.id.checkpoint);
speciesEditText.setId(layout.getChildCount());
// Perform whatever required over EditText, same way you can add more elements.
layout.addView(oView);
}
});
Hope it will help.
I have a list of articles in a listView with text on the left, and an image on the right. If the article doesn't have an image, I'd like the text to go full-width (as opposed to having a marginRight of 60dp).
My noob-java thought process is: I'm already looping through and checking if there's an image... (if there is one, I change which image shows up) within that loop, can I somehow use java to alter the XML of my view to add or remove the imageView and add or remove the margin?
The code I'm using to repeat through and alter the image:
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage("", viewHolder.thumbView); //clears previous one
if(article.filepath != null && article.filepath.length() != 0) {
imageLoader.displayImage(
"http://img.mysite.com/processes/resize.php?image=" + article.filepath + "&size=100&quality=70",
viewHolder.thumbView
);
}
My "article_entry_list_adapter.xml":
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for individual news entries in a list -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp" >
<!-- Title of the news entry -->
<TextView
android:id="#+id/article_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="60dp"
android:textSize="13sp"
android:textStyle="bold"
android:textColor="#drawable/list_title_selector" android:typeface="serif"/>
<!-- Subtitle contains author and date -->
<TextView
android:id="#+id/article_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="60dp"
android:layout_alignLeft="#id/article_title"
android:layout_below="#id/article_title"
android:textSize="11sp"
android:textColor="#drawable/list_subtitle_selector" />
<com.sltrib.views.WebImageView
android:id="#+id/article_thumbnail"
android:layout_width="50dp"
android:layout_height="50dp"
android:adjustViewBounds="true"
android:layout_alignParentRight="true"
android:background="#color/super_light_gray"
android:padding="1dp"
android:scaleType="centerCrop" android:cropToPadding="true"/>
</RelativeLayout>
I would suggest changing your xml layout to something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp" >
<!-- Title of the news entry -->
<com.sltrib.views.WebImageView
android:id="#+id/article_thumbnail"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:adjustViewBounds="true"
android:background="#color/super_light_gray"
android:cropToPadding="true"
android:padding="1dp"
android:scaleType="centerCrop"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:layout_toLeftOf="#id/article_thumbnail"
android:orientation="vertical" >
<TextView
android:id="#+id/article_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Titol"
android:textColor="#drawable/list_title_selector"
android:textSize="13sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="#+id/article_subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Subtitol"
android:textColor="#drawable/list_subtitle_selector"
android:textSize="11sp" />
</LinearLayout>
Then, when there is no image, just set the WebImageView visibility to GONE and the text will take the parent's width.
With your example:
viewHolder.thumbView.setVisibility(View.VISIBLE); //to show the image
viewHolder.thumbView.setVisibility(View.GONE); //to hide the image
You can either alter your viewGroup (setting visibility of your imageView to GONE and adding a margin programmatically) or choose to inflate another xml. As you want to do =)
Just while looping setVisiblity(VIEW.Gone) of article_thumbnail.
TextView textView = (TextView) findViewById(R.id.textView1);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(50, 0, 0, 0); // llp.setMargins(left, top, right, bottom);
textView.setLayoutParams(llp);