I have added a Button to my WebView. But I cannot get it to align to bottom of screen. I have given the code below.
Button button = new Button(context);
button.setText("Button");
LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
button.setLayoutParams(params1);
webView.addView(button);
Does anyone know how to do this.
For your concern try like this :;
Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setGravity(Gravity.BOTTOM);
UPDATE :: Try some thing like this for the button U need to place at bottom of screen
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center|bottom"
android:orientation="vertical" >
<Button
android:id="#+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:text="3" />
</LinearLayout>
You can try with an LinearLayout and gravity :
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity=Gravity.BOTTOM;
Ok I got the solution. I did button.setY(screenHeight-button.getHeight());
Related
SOLVED - The ID of a View-Element should not be set to 0 !!!
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"/>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/Button1" />
<Button
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:layout_below="#+id/Button1"/>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_toRightOf="#+id/Button2"/>
<Button
android:id="#+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:layout_below="#+id/Button2"/>
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_toRightOf="#+id/Button3"/>
</RelativeLayout>
Now that is my Code in the Activity, i tried to to exactly what i did in the xml, but the items are displayed completely different:
public class MainActivity extends Activity {
private RelativeLayout relativeLayout;
public RelativeLayout.LayoutParams layoutParams;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//button 1
Button button1 = new Button(this);
button1.setText("Button 1");
button1.setId(0);
relativeLayout.addView(button1);
//editText1
EditText editText1 = new EditText(this);
editText1.setId(1);
layoutParams.addRule(RelativeLayout.RIGHT_OF, 0);
relativeLayout.addView(editText1, layoutParams);
layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// button2
Button button2 = new Button(this);
button2.setText("Button 2");
button2.setId(2);
layoutParams.addRule(RelativeLayout.BELOW, 0);
relativeLayout.addView(button2, layoutParams);
layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//editText2
EditText editText2 = new EditText(this);
editText2.setId(3);
layoutParams.addRule(RelativeLayout.BELOW, 1);
layoutParams.addRule(RelativeLayout.RIGHT_OF, 2);
relativeLayout.addView(editText2, layoutParams);
layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
this.setContentView(relativeLayout);
}
}
SOLVED:
The main issue is, that an id of 0 for a view-element is not allowed...erm yea, beginner's mistake, i know...
THANK you anyone for helping on this question! SOLVED!!!
Try this! It converts xml to java code. However, I am not quite sure if you really need it in this case...
You're doing all these backwards
relativeLayout.addView(button2, layoutParams);
layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Should be
layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
button2.setLayoutParams(layoutParams);
relativeLayout.addView(button2);
You may need to do more to get it the way you want, but why are you wanting to do this? If it works why change it?
you have already added the buttons to the xml file why are you creating dynamic buttons instead inflate the view if you want to add the layout dynamically also when you dynamically add the buttons to the relative layout you must call addRule function to place them at right place
just create objects of button and edittext refer the as you have refere your relative layout
relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
and use them.
Edit----
For that you must remove buttons from the xml layout and then create the buttons like you are doing it's prefect just one thing like this here
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"/>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
**android:layout_toRightOf="#+id/Button1"** />
to place this editText you must use addRule() function
EditText editText1 = new EditText(this);
editText1.setId(1);
layoutParams.addRule(RelativeLayout.RIGHT_OF, 0);
relativeLayout.addView(editText1, layoutParams);
layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams .addRule(RelativeLayout.RIGHT_OF, 0);
where 0 is your button's id also set the layout params too to the layout like
editText.setLayoutParams(layoutParams );
I have asked this before but no answers that worked for me.
How to move a image View left and right in android development
Please use this code:
In main.xml:
<ImageView
android:id="#+id/imageview"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left"
android:onClick="onClickLeft"/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right"
android:onClick="onClickRight"/>
And then in the .java file put this code:
public void onClickLeft(View view){
ImageView t = (ImageView) findViewById(R.id.imageview);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity=3;
t.setLayoutParams(params);
}
public void onClickRight(View view){
ImageView t = (ImageView) findViewById(R.id.imageview);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity=5;
t.setLayoutParams(params);
}
This will give you what you are looking for.
I developed one Simple app, which contains one textview.and my problem is that I want visible in invisible this text view on button click event.
At loadtime I do this
myTextView.setVisible(View.GONE);
and after this at Button Click event, I do this.
myTextView.setVisible(View.VISIBLE);
The textview is visible but it is overlap on below TextView means myTextView cannot contain space. So what can I do now?
use
myTextView.setVisible(View.INVISIBLE);
instead of
myTextView.setVisible(View.GONE);
to persist the space in layout.........
If you are using relativelayout, specify android:layout_below="id_of_above_text_view" in the second textview.
If you dont specify the relation to the views in relative layout, it will appear one above other
try this:
RelativeLayout layout = new RelativeLayout(this);
TextView tv1 = new TextView(this);
tv1.setText("A");
tv1.setId(1);
TextView tv2 = new TextView(this);
tv2.setText("B");
tv2.setId(2);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.BELOW, tv1.getId());
layout.addView(tv1);
layout.addView(tv2, lp);
and when you change visibility, call invalidate() on parent view of myTextView (layout here) or root view of your layout ( layout.getRootView() )
A slight change in your code should make it work fine I believe.
myTextView.setVisibility(View.INVISIBLE);
and after the button click,
myTextView.setVisibility(View.VISIBLE);
For doing it in the java code, have a go at this(haven't tried it out myself though)...
RelativeLayout rl = new RelativeLayout(this);
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.xxx);
TextView txt = new TextView(this);
txt.setText("XXX");
rl.addView(iv,0);
rl.addView(txt,1);
This will show and hide the text(s) when the corresponding buttons are pressed.
Very useful if you want to control multiple text with buttons
1)XML File:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="vertical"
android:gravity="center_vertical">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:text="The Origin"
android:id="#+id/btnOrigin"
android:onClick="buttonOnClick"/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:id="#+id/txtOrigin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/about"
android:textColor="#000"
android:textSize="#dimen/text_body"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:visibility="gone"/>
</TableRow>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="vertical"
android:gravity="center_vertical">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:text="Vision"
android:id="#+id/btnVision"
android:onClick="buttonOnClick"/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:id="#+id/txtVision"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/vision"
android:textColor="#000"
android:textSize="#dimen/text_body"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:visibility="gone"/>
</TableRow>
</LinearLayout>
2) JAVA code
private TextView txtOrigin, txtVision;
public void buttonOnClick(View view) {
switch (view.getId()) {
case R.id.btnOrigin:
txtOrigin = (TextView) findViewById(R.id.txtOrigin);
txtOrigin.setVisibility(View.VISIBLE);
txtVision = (TextView) findViewById(R.id.txtVision);
txtVision.setVisibility(View.INVISIBLE);
break;
case R.id.btnVision:
txtVision = (TextView) findViewById(R.id.txtVision);
txtVision.setVisibility(View.VISIBLE);
txtOrigin = (TextView) findViewById(R.id.txtOrigin);
txtOrigin.setVisibility(View.INVISIBLE);
break;
}
}
i have in xml tis code:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/examp1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/examp2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/examp1" />
</RelativeLayout>
I want to create these buttons programmatically and i use:
RelativeLayout mainLayout;
mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
for (int i=0; i<1; i++)
{
Button new_button = new Button(this);
new_button.setText("blabla");
new_button.setId(i);
new_button.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
mainLayout.addView(new_button);
}
How can I translate this command android:layout_below="#id/examp1" programmatically?
Use RelativeLayout.LayoutParams. Use addRule(), for your example use it with RelativeLayout.BELOW and the id (R.id.examp1)
I hope question title gives you a good description of the problem.
I want to create this XML, but programatically (please don't suggest not doing it programmatically ^_^ )
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:text="Messages:"/>
<EditText android:id="#+id/messageHistory"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="false"
android:layout_weight="1"
android:editable="false"
android:gravity="top"
android:scrollbars="vertical"
android:scrollbarSize="10px"
/>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="4">
<EditText android:id="#+id/message"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
android:layout_weight="1"
/>
<Button android:id="#+id/sendMessageButton"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="4"
android:text="Send"/>
</LinearLayout>
As you can see, there are two LinearLayout's, one embedded in the other. I need to reproduce this and then add it to a ViewFlipper.
This is what I have so far:
LinearLayout l1 = new LinearLayout(this);
LinearLayout l2 = new LinearLayout(this);
Button btn=new Button(this);
EditText messageHistory = new EditText(this);
EditText newMessage = new EditText(this);
TextView windowTitle = new TextView(this);
btn.setText("Send");
btn.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 4f));
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Clicked send in child index: "+ flipper.getDisplayedChild(), Toast.LENGTH_SHORT).show();
}
});
windowTitle.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
windowTitle.setPadding(0, 5, 0, 10);
windowTitle.setText("Chat with: ");
messageHistory.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
messageHistory.setGravity(Gravity.TOP);
messageHistory.setMovementMethod(new ScrollingMovementMethod());
messageHistory.setClickable(false);
messageHistory.setFocusable(false);
messageHistory.setPadding(0, 0, 0, 5);
newMessage.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f));
newMessage.setGravity(Gravity.TOP);
newMessage.setMovementMethod(new ScrollingMovementMethod());
newMessage.requestFocus();
l1.setOrientation(LinearLayout.VERTICAL);
l1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
l2.setOrientation(LinearLayout.HORIZONTAL);
l2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
l1.addView(windowTitle);
l1.addView(messageHistory);
l2.addView(newMessage);
l2.addView(btn);
l1.addView(l2);
flipper.addView(l1);
Where flipper is defined as:
ViewFlipper flipper = (ViewFlipper) findViewById(R.id.viewflip);
Without a problem, I can see "l1" in the window when it loads up, but l2 is no where to be found. Did I mess up with LayoutParams? Can I use addView to add a LinearLayout?
I think you forgot to set layout weight (only my opinion though, could be wrong).
because you are adding more views with layout-height set to FILL_PARENT