So, I'm trying to make a dynamic UI, and i want to add a seperator to it. unfortunately, i could only find out how do one in XML. is it possible to turn this
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/seperator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:scaleType="fitXY"
android:src="#android:drawable/divider_horizontal_dark" />
into program code?
my best attempt was
ImageView seperator=new ImageView(this);
seperator.setImageDrawable(drawable.divider_horizontal_dark);
Put it in an extra layout file and inflate it when you need it in code - I think that what you want to do and should be the easiest way.
In your Activity:
View v = LayoutInflater.from(this).inflate(R.layout.seperator, null);
If you inflate a Layout:
LinearLayout ll = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.custom_layout, null);
TextView tv = (TextView) ll.findViewById(R.id.tv);
There is a website can convert that for you. You can design the interface with eclipse then submit the generated xml to XMLtoJAVA online converter and it should do it for you..
You can also create a View, define a background and add it with a LayoutParams
ViewGroup container = (ViewGroup) findViewById(R.id.container);
View separator = new View(context);
separator.setBackgroundColor(Color.Black);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, 2);
container.addView(separator, layoutParams);
Related
We are using android API 17 in our application. I have defined a layout containing two images vies as below:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/image_container_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image_1_resource"/>
<ImageView
android:id="#+id/image_2"
android:layout_marginTop="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/image_container_layout"
android:src="#drawable/image_2_resource"/>
This layout is included inside another layout as below:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#style/wizard_content_style"
tools:context=".ui.Wizard"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
>
<include layout="#layout/image_container_layout"
android:id="#+id/included_view"
/>
<TextView
style="#style/wizard_content_text_style_medium"
android:id="#+id/text_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/included_view"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:text="#string/instruction"
android:layout_marginBottom="15dp"/>
The reason that the layout is included is that we want to reuse it in two more layouts.
Now based on some condition I want to hide or show the image views inside image_container_layout.
The java code looks like this:
containerLayout = (ViewGroup) ((Activity) getAndroidContext()).getLayoutInflater().inflate(R.layout.image_container_layout, null);
image1 = (ImageView) containerLayout.findViewById(R.id.image_1);
image2 = (ImageView) containerLayout.findViewById(R.id.image_2);
switch (accuracy) {
case 1:
log().i(getClass().getSimpleName(), "case 1 chosen");
image1.setVisibility(View.VISIBLE);
image2.setVisibility(View.GONE);
log().i(getClass().getSimpleName(), "image 1 has been shown");
break;
case 2:
image1.setVisibility(View.GONE);
image2.setVisibility(View.VISIBLE);
break;
case 3:
image1.setVisibility(View.VISIBLE);
image2.setVisibility(View.VISIBLE);
break;
}
I am debugging this code and I am sure the code is running. The log messages are printed in Logcat as well, but nothing happens no change in the images. Also, both images are always shown.
I wonder if there is something that I have to do when working with the included layout?
Thanks for any help in advance.
Based on answers I got below, seems that inflating a view will create a new object and because of this, changes in the visibility are not shown on the user interface.
Then the question is that if we have a wizard and inside 3 different pages of the wizard I want to have an image and depending on some condition I want to show or hide the image, what is the best solution? I mean I want to reuse the code which is responsible for hiding and showing the image regardless which page of wizard is active.
Why are you complexing with so much code. If you include some layout in your xml then you can use those widgets also same as the xml have. There is no need to inflate.
ImageView image_2 = findViewById(R.id.image_2);
image_2.setVisbility(Visible.GONE);
You said at this comment the code not inside activity but wherever it is you inflated a new layout to your view currently displaying by this line:
containerLayout = (ViewGroup) ((Activity) getAndroidContext()).getLayoutInflater().inflate(R.layout.image_container_layout, null);
When you try to change visibility of those images actually it works, i think so. But if your activity or fragment layout contains image_container_layout maybe you see
those images.
And I wonder that what do you do with inflated view containerLayout. Do you add it to inside of any other view. If you dont it wont be visible for you.
you have to use it like this:
View included_view1 = findViewById(R.id.included_view1);
ImageView image_1 = included_view1.findViewById(R.id.image_1);
ImageView image_2 = included_view1.findViewById(R.id.image_2);
image_1.setVisibility(View.VISIBLE);
image_1.setVisibility(View.GONE);
image_2.setVisibility(View.VISIBLE)
image_2.setVisibility(View.GONE)
View included_view2 = findViewById(R.id.included_view2);
ImageView image_11 = included_view2.findViewById(R.id.image_1);
ImageView image_22 = included_view2.findViewById(R.id.image_2);
image_11.setVisibility(View.VISIBLE);
image_11.setVisibility(View.GONE);
image_22.setVisibility(View.VISIBLE)
image_22.setVisibility(View.GONE)
Above code will be helpful in the case of multiple time you want to use same layout.
I'm trying to format the three Strings into an android button. The first string should be about 30% of the button, second should be about 50%, and the rest to the third button. Each text should be contrained within a certain length. How can I do this?
After searching through many possible solutions, what I came up with was to create 3 different buttons and put them together. This wouldn't be ideal but it's a start. Are there better ways to do this?
This is what I have tried so far:
The Java:
LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout_buttons);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
Button addButton = new Button(this);
Button numPlayerButton = new Button(this);
Button lobbyButton = new Button(this);
Button locationButton = new Button(this);
ll.addView(addButton, lp);
ll.addView(numPlayerButton, lp);
ll.addView(lobbyButton, lp);
ll.addView(locationButton, lp);
XML:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/linearlayout_buttonlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearlayout_buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- THIS IS WHERE THE BUTTONS GO!!!!!! -->
</LinearLayout>
</LinearLayout>
</ScrollView>
My goal with this code was to have the buttons created horizontally with the inner linear layout, and I thought the outer linear layout will create the next iteration of buttons on the next line (vertically). I have the buttons created programmatically through a loop.
If you want to click a widget then do something(as a button does), you can use a Layout and add a onClickListener to it. In this Layout, you can add strings as you want.
Simply do it like that:
Write a RelativeLayout in your XML and give it a id.
Add three textViews in RelativeLayout and place them as you want.
Register this RelativeLayout in Java and add a onClickListener to it.
For Java part:
RelativeLayout ThreeStringLayout = (RelativeLayout) view.findViewById(R.id.three_string_layout);
ThreeStringLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view1) {
//do things here
}
});
I implemented it in a Fragment, you need to modify it if you use it in Activity.
Good Luck.
This cannot be achieved using a standard Android Button (that I know of). You will have to use a linear layout with textviews. I can explain how that would work if need help
I have created a TextView dynamically and added it to a linear layout. The TextView is supposed to have an icon on its left. I have added the icon and wanted the TextView to be in the center.
The code:
TextView valueTV = new TextView(getContext());
valueTV.setText(richPost.getPostCreateDate());
valueTV.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_launcher, 0, 0, 0);
valueTV.setGravity(Gravity.CENTER);
llInner.addView(valueTV);
The output:
I want the image to be right before the text and want to remove the huge gap in between. The text is in the proper place, I want the image to be centered beside the text. Where am I going wrong? What should I do?
First, make yoúr TextView wrap its content by adding this to your code:
valueTV.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Additionally, to get the icon and text centered in the LiniarLayout, which you can do by adding this to the LinearLayout:
yourLinearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
yourLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
This may be caused by TextView layout parameters. It seems to me that in your case width of TextView is match_parent, so the drawableLeft position is defined by left side of LinearLayout.
Here is simple example to feel the difference
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:drawableLeft="#drawable/ic_send_white_24dp"
android:drawablePadding="8dp"
android:text="some text"/>
</LinearLayout>
and try the same layout with match_parent width of TextView
I'm stuck on a problem and I don't know, what causes it.
I have a very simple Layout like this:
<?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="vertical">
<TextView
android:id="#+id/my_text_view"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="NOTE_THIS"
android:textColor="#FFFFFF"
android:textSize="22dp"
android:text="TestText"/>
</LinearLayout>
which is included inside another layout. If I change the gravity inside the xml I see same Result in Layout-Editor and on my phone. If I wanna apply the Gravity programatically like with myTextView.setGravity(Gravity.CENTER) it doesn't Change anything. And I cannot set LayoutGravity in Java on a TextView
I'll tried for debug purposes to include them three times each with another gravity which does work even. So I assume everything is alright with my Layout and there has to be a Bug or something else I miss.
Can someone give me a hint what I also can try, or what causes this problem?
Set your TextView's width as android:layout_width="fill_parent" then you can set it programmatically using myTextView.setGravity(Gravity.CENTER)
You need to set the gravity of the LayoutParams object instead of the View itself:
TextView tv = new TextView(getApplicationContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
lp.gravity = Gravity.CENTER;
tv.setLayoutParams(lp);
When you use LinearLayout as parent, then layout_gravity comes in picture which align control but not content inside the control so,
Instead using android:layout_gravity use android:gravity.
I would like to know if is there a way to call android:layout_gravity property from a Java method. I didn't found any method in Android documentation to do it. This is the picture of the layout I want to implement:
http://www.anddev.org/resources/image/2234
I know to do it through XML, as following:
<FrameLayout
xlmns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_gravity="left|center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="<" />
<Button
android:layout_gravity="right|center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=">" />
</FrameLayout>
But in my situation, I need to do it through Java code, because I'll implement another layout views dynamically. To avoid merging XML layout with Java code, I would prefer make all layout using Java.
Well as far as I understand you looking for this It is for FrameLayout for other layout see appropriate LayoutParams classes. For setting gravity like in your XML use something like:
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.LEFT | Gravity.CENTER_VERTICAL)
followed by addView with newly constructed LayoutParams
Maybe I misunderstand you, but here it is:
http://developer.android.com/reference/android/widget/TextView.html#setGravity(int)
Watchout! This wouldn't work with LinearLayout though. Because LinearLayout.LayoutParams(...) constructor is different from FrameLayout.LayoutParams(...) constructor. The third parameter is not gravity but weight.
LinearLayout.LayoutParams(int width, int height, float weight)
as opposed to
FrameLayout.LayoutParams(int width, int height, int gravity)
and this call, despite not producing compiler error is actually wrong:
lparams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.LEFT);
button.setBackgroundResource(R.drawable.sound_on);
button.setText("On");
button.setGravity(Gravity.CENTER_VERTICAL| Gravity.RIGHT);
button.invalidate();
To change the layout_gravity attribute for an existing view:
Button button = (Button) findViewById(R.id.some_button);
FrameLayout.LayoutParams params;
params = (LayoutParams) button.getLayoutParams();
params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
button.setLayoutParams(params);
The button is contained in a FrameLayout which has a gravity attribute corresponding to the layout_gravity XML attribute. Documentation link.
before adding the button, you should change the orientation of your layout
yourbutton.setGravity(Gravity.RIGHT);
LayoutParams layout = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
layout.gravity = Gravity.RIGHT;
yourbutton.setLayoutParams(layout);
yourlayout.setGravity(Gravity.RIGHT);
yourlayout.addView(yourbutton);