I am creating my controls programmatically, based on JSON received from a server. One of the controls I need to create is a WebView that is horizontally centred on the screen. This is simple in xml layouts as shown below using the layout_gravity option. But how do you do this in code, the WebView unlike TextView does not have a setGravity(Gravity.HORIZONTAL_GRAVITY_MASK) method.
<?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" >
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
I would use a RelativeLayout, then you can use LayoutParams when adding the view:
RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
relativeLayout.addView(yourWebView, layoutParams);
i use LinearLayout to show webview and setGravity.
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
linearLayout.setGravity(Gravity.CENTER);
WebView view = new WebView(this);
linearLayout.addView(view);
setContentView(linearLayout);
it help you.
Related
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = new RelativeLayout(this);
setContentView(layout);
ScrollView sc = new ScrollView(this);
sc.setBackgroundColor(Color.YELLOW);
sc.setLayoutParams(layoutParams(300, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.ALIGN_PARENT_RIGHT));
RelativeLayout rl = new RelativeLayout(this);
rl.setBackgroundColor(Color.CYAN);
rl.setLayoutParams(layoutParams(300, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.ALIGN_PARENT_RIGHT));
int k=0;
for(int i=0;i<15;i++){
ImageView iv1 = new ImageView(this);
iv1.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
iv1.setLayoutParams(layoutParams(200, 200,0));
iv1.setTranslationY(k);
k+=200;
rl.addView(iv1);
}
sc.addView(rl);
layout.addView(sc);
}
public RelativeLayout.LayoutParams layoutParams(int width,int height,int rule1){
RelativeLayout.LayoutParams lpb = new RelativeLayout.LayoutParams(width,height);
if(rule1 != 0){
lpb.addRule(rule1);
}
return lpb;
}
Output of code
when i'm using RelativeLayout without ScrollView then it is showing all images but when i'm using ScrollView then it is not showing all images.
Please check where is the problem in the code.
it should be in right alignment inside ScrollView.
Desired output
XML Code of desired output ,which i'm trying to convert in java code.
<ScrollView
android:layout_width="300px"
android:layout_height="match_parent"
android:layout_alignParentRight="true" >
<RelativeLayout
android:layout_width="300px"
android:layout_height="fill_parent"
android:background="#android:color/holo_purple" >
<ImageView
android:id="#+id/iv1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/ic_action_home" />
<ImageView
android:id="#+id/iv2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#+id/iv1"
android:src="#drawable/ic_action_home" />
<ImageView
android:id="#+id/iv3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#+id/iv2"
android:src="#drawable/ic_action_home" />
<ImageView
android:id="#+id/iv4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#+id/iv3"
android:src="#drawable/ic_action_home" />
</RelativeLayout>
</ScrollView>
A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects.
So when you are using Relative Layout it is displaying images but when your are using scroll view which doesnot contain all the images under one layout it doesnot work, so try putting all images under LinearLayout (recommend) or other layouts.
I am inflating a view in my activty like this
View myView = getLayoutInflater().inflate(R.layout.my_view, my_activity_layout);
And now I want to center myView in the regular layout
I tried this
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams)myView.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
myView.setLayoutParams(layoutParams);
But got this error
android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
How could I center this view?
Thanks
EDIT
Here is my xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp" android:layout_height="250dp"
android:id="#+id/discountView" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Discount:"
android:id="#+id/discountTitleText"
android:layout_marginTop="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:textColor="#ffffff" />
Replace FrameLayout.LayoutParams instead if RelativeLayout.LayoutParams.
OR
You have to change FrameLayout to RelativeLayout in my_view.xml file
What is error here? To understand it you need to understand how view/viewgroup hierarchy is created for that refer : http://android-developers.blogspot.in/2009/03/android-layout-tricks-3-optimize-by.html
So if you go back to your view actually you have FrameLayout-->RelativeLayout-->Textview
Solution
Instead of use
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams)myView.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
myView.setLayoutParams(layoutParams);
use
FrameLayout.LayoutParams layoutParams =
(FrameLayout.LayoutParams)myView.getLayoutParams();
layoutParams.addRule(//your code);
myView.setLayoutParams(layoutParams);
or alternatively get RelativeLayout from your view
RelativeLayout rootLayout = myView.findViewById(R.id.discountView);
//And using RelativeLayout params as you have done
rootLayout.setLayoutParams(layoutParams)
I have created form which contain question text and input field suitable to question. I have spinner ,edittext and checkbox in my form.
All are created dynamically using activity . Problem in the created layout is that input field displayed correctly but textview not displayed.
private void showIssueForm(){
for (int i=0;i<claimQuestionList.size();i++){
ClaimQuestion claimQuestion=claimQuestionList.get(i);
LinearLayout linearLayout=ClaimUtils.createLayoutForClaimQuestion(claimQuestion,this);
/*claimQuestionsLinearLayout.addView(linearLayout);*/
claimQuestionsLinearLayout.addView(linearLayout,i);
}
}
private static TextView createTextView(ClaimQuestion claimQuestion,Context context){
TextView textView=new TextView(context);
LinearLayout.LayoutParams textViewLayoutParams=new LinearLayout.LayoutParams(300,80,1);
textView.setText("hi m printing the text what are you looking for");//claimQuestion.getOrder()+". "+claimQuestion.getLabel());
textView.setLayoutParams(textViewLayoutParams);
//textView.setMinLines(1);
// textView.setSingleLine(false);
textView.setTextSize(R.dimen.system12);
textView.setVisibility(View.VISIBLE);
textView.setTextColor(context.getResources().getColor(R.color.light_white_));
return textView;
}
The linear layout where i added form element is given below
public static LinearLayout createLayoutForClaimQuestion(ClaimQuestion claimQuestion,Context context){
LinearLayout linearLayout=new LinearLayout(context);
LinearLayout.LayoutParams linearLayoutParams=new LinearLayout.LayoutParams(500,200);
linearLayout.setLayoutParams(linearLayoutParams);
linearLayout.setOrientation(LinearLayout.VERTICAL);
TextView questionLabel=createTextView(claimQuestion,context);
linearLayout.addView(questionLabel,0);
if (claimQuestion.getQuestionType()==ClaimQuestion.QuestionTypeEnum.CQ_TEXT){
EditText editText=createEditText(claimQuestion,context);
linearLayout.addView(editText,1);
}else if(claimQuestion.getQuestionType()==ClaimQuestion.QuestionTypeEnum.CQ_CHECKBOX){
CheckBox checkBox=createCheckBox(claimQuestion,context);
linearLayout.addView(checkBox,1);
}else if(claimQuestion.getQuestionType()==ClaimQuestion.QuestionTypeEnum.CQ_SELECT){
Spinner spinner=createSpinner(claimQuestion,context);
linearLayout.addView(spinner,1);
}
return linearLayout;
}
The xml layout file for the parent layout is
<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="com.maximus.mycar.VehicleSelectActivity"
android:background="#drawable/bluebg_320_568">
<include layout="#layout/header"
android:id="#+id/top_header_layout_ici"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="10dp"/>
<ScrollView
android:id="#+id/claim_questions_scroll_view"
android:layout_below="#id/top_header_layout_ici"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginBottom="20dp"
>
<LinearLayout
android:id="#+id/claim_questions_linear_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
I Changed the font size by setting fixed value then it worked fine.
textView.setTextSize(12);
use this one:-
textView.setTextColor(getResources().getColor(R.color.light_white_));
instead of this:-
textView.setTextColor(context.getResources().getColor(R.color.light_white_));
please let me know if it works or not :)
I'm building a contacts app which displays big contact photos. I need to add a label with the contacts name of each contact on top of the button (near the bottom) however I don't know how to get two views on top of each other. I cannot simply use settext since I need to add a semi-transparent background to the label.
EDIT:
I managed to get it on top but I cannot figure out how to get it on the bottom of the button.
RelativeLayout icon = new RelativeLayout(context);
// Create button
Button button = new Button(context);
button.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
layout.addView(button);
// Create label
TextView label = new TextView(context);
label.setText(name);
label.setBackgroundColor(Color.argb(120, 0, 0, 0));
label.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
icon.addView(button);
icon.addView(label);
However the text appears on the top of the image and I want it to be on the bottom like this:
With xml this would be something like: android:layout_alignBottom="#+id/myButton" but I'm doing this programatically and I haven't found a way to do it. How can I place my label near the button?
Try this
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<Button
android:id="#+id/button1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="right"
android:text="John"
android:background="#drawable/button_action_active" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test textView" />
</FrameLayout>
</LinearLayout>
Dynamically you can do by
TextView lable = new TextView(this);
lable.setLayoutParams(new LayoutParams(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
lable.setTextSize(25);
lable.setGravity(Gravity.CENTER);
lable.setText("John");
Button button = new Button(this);
button.setBackgroundResource(R.drawable.button_action_active);
button.setLayoutParams(new LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
FrameLayout fl = new FrameLayout(this);
fl.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
fl.addView(button);
fl.addView(lable);
setContentView(fl);
To do this you can use a frame layout. The documentation for frame layout can be found here - http://developer.android.com/reference/android/widget/FrameLayout.html
However, frame layout is depreciated (if I remember correctly). So instead I would recommend using a relative layout. In your relative layout you can set the position of the button and then give the textview the attributes android:layout_alignLeft=#id/somethingand android:layout_alignRight="#id/Something"
I have seen many posts regarding dynamically adding table rows, but I am not sure what I'm missing.
When I execute the following, nothing is displayed (aside from application title bar).
My Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/table_view_test_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ScrollView
android:id="#+id/tvt_scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
>
<RelativeLayout
android:id="#+id/tvt_scroll_relative"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:id="#+id/tvt_tableview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</TableLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
My Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table_view);
TableLayout tableLayout = (TableLayout) findViewById(R.id.tvt_tableview);
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView column1 = new TextView(this);
column1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
column1.setBackgroundColor(Color.YELLOW);
column1.setTextColor(Color.BLUE);
column1.setText("Col1 Value");
tableRow.addView(column1);
TextView column2 = new TextView(this);
column2.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
column2.setBackgroundColor(Color.RED);
column2.setTextColor(Color.GREEN);
column2.setText("Col2 Value");
tableRow.addView(column2);
tableLayout.addView(tableRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
//tl.refreshDrawableState();
//findViewById(R.id.tvt_scroll_relative).refreshDrawableState();
}
Change the two references
ViewGroup.LayoutParams
to
TableRow.LayoutParams
and it should work.
Adding layout params to a widget requires that the LayoutParams are a inner class of the surrounding layout container.
I think this has something to do with you setting the layout params to the TextViews. If you remove those definitions the info appears. Also if you take a look at the official documentation for TableRow you can see:
The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT.