How to make a view as always on top? - android

I have a TextView and it is on an image. When I click the button, TextView's background color will change, but image won't disappear. For example:
My TextView at the beginning:
When I click a button:

If you want to do this using only one TextView then its may be not possible. So, I will suggest you to do this using FrameLayout. you can write your layout as below.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</FrameLayout>
When you want to change the color behind the TextView then change the ImageView background as below...
ImageView mageView view = (ImageView) findViewById(R.id.image);
view.setBackgroundColor(Color.BLUE);

If it must be a TextView and an ImageView you could also wrap it within a FrameLayout (which is supposed to contain only one child). A FrameLayout will place all containing children elements on the same "place", so the get overlapped.
Or when you need more elements, you could also use a RelativeLayout and give your elements the same layout rules (e.g. all centered).

You should use ImageView instead of TextView, the:
public void onClick(View v){
imageView.setBackgroundColor(Color.GREEN);
}

ImageButton is better option for you.Image source will be star as u said.then onclick you change the background. if want set Text in this
android:drawableTop="#drawable/any_drawable"
android:text="#string/any_text"

Related

Android: how to fit EditText's RightView according to the EditText height

I need to set an icon (search icon) to a EditText's RightView, I've used the following xml:
android:drawableRight="#drawable/search_icon"
The problem is that the RightView doesn't fit inside the EditText, instead the EditText's height changes based on the icon height.. Is it possible to do the opposite, to edit the RightView's height based on the EditText's height?
XML of the EditText:
<EditText
android:id="#+id/regSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="18dp"
android:layout_marginLeft="45dp"
android:layout_marginRight="45dp"
android:ems="10"
android:hint="Search Test"
android:inputType="phone"
android:padding="5dip"
android:textColor="#color/lowBlack"
android:textSize="16sp"
android:drawableRight="#drawable/search_icon" />
Instead of using drawableRight, you can tweak your layout a little bit and have an imageview alongside the edittext, and then you can play with the height and width of the two views. e.g. this can be achieved using a RelativeLayout with the edittext and imageview within:
<RelativeLayout>
<EditText android:id="my_edittext"/>
<ImageView android:toRightOf="#+id/my_edittext"
android:layout_alignTop="#+id/my_edittext"
android:layout_alignBottom="#+id/my_edittext"/>
<RelativeLayout/>
The alignTop and alignBottom attribute will take care of the height issue you are facing. And toRightOf will put the imageview to the right of the edittext in your layout.
But if you want the imgeview to be over the edittext, you can achieve that too using this layout - just remove the "toRightOf" attribute and add alignParentRight="true" to the ImageView.
NOTE: I've not written the complete xml code here. Let me know if you need any more help with this.
Finally after searching for a while I've came up with the following solution that doesn't need to create any other "hackish" element:
regSearch.getViewTreeObserver()
.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#SuppressWarnings("deprecation")
#Override
public void onGlobalLayout() {
Drawable img = Registrazione.this.getResources().getDrawable(
R.drawable.search_icon);
img.setBounds(0, 0, regSearch.getMeasuredHeight()-regSearch.getMeasuredHeight()/3, regSearch.getMeasuredHeight()-regSearch.getMeasuredHeight()/3); //you can edit this in order to resize the drawable
regSearch.setCompoundDrawables(null, null, img, null);
regSearch.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
This way you can set and edit the dimension of whatever drawable you want, hope it can help someone else because I got stuck on this problem for a while :P

Android: ImageButton with a dynamic size and fixed position

In my app I want set an ImageButton in a position in this way
and I want adapt this button in this way for all screen, is there a strategy to obtain it?
It depends on how many screens you have that should have this button, how they interact with each other and whether the button should have the same funtion in every screen, but one option would be to have only one Activity with a layout, where you have the button on the bottom and an empty Layout above the button that works as fragment-container. Instead of starting new activities for a new "screen" change the Fragment in this container. The Button is always at the same place and you can handle its onClick centrally in the Activity...
Use
android:align = "Center"
or
android:width ="0dp"
android:weight = "1"
BPut ImageView in RelativeLayout and set attrs for image view like that:
<RelativeLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/previewImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>

how to fix this imageview error?

Allow me to explain. I have :
a button with picture (located at #drawable/pic),
linear layout (id=linear1)
the button xml is below :
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginRight="10dp"
android:scaleType="fitXY"
android:src="#drawable/pic"
android:maxWidth="80dp"
android:maxHeight="80dp"
tools:ignore="ContentDescription" />
the linear layout xml is as follow :
<LinearLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="center"
android:orientation="horizontal"
tools:ignore="UselessLeaf" >
what i want is, when i click the button i want to create/generate imageview programatically inside the linearlayout and i want to fill it with the same picture for the button (pic). The code is below :
//initiate imageview
ImageView img=new ImageView(this);
//get drawable from button
Drawable blabla=btn1.getDrawable();
//set drawable to imageview
img.setImageDrawable(blabla);
//set height and width of imageview to 50dp
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(50,50);
img.setLayoutParams(parms);
img.setScaleType(ScaleType.FIT_XY);
//place imageview to linearlayout
layoutTempat.addView(img);
The code works fine with displaying the imageview with the same image as the button have.
however the problem is : when i set the imageview to 50dp programatically, the image inside button changed too.. how can it happened ? i am so confused as iam a newbie..
Thanks before.
The two views are sharing the same drawable.
It's plausible your manipulations on one view are being sent to the underlying drawable, effecting how its displayed in the other view -- frankly I don't know. But assuming the case is as you described, this problem is easily fixed by cloning the drawable as follows:
Drawable dr = btn1.getDrawable().getConstantState().newDrawable();
img.setImageDrawable(dr);

Android: put something on top of linear layout

What I want to do is show a "frame" (or new layout) on top of "2" (second LinearLayout), when a button would be pressed. How should I do it? Precreate it and make it somehow hidden if button not pressed?
I have this type of layout:
XML:
<LinearLayout>
<LinearLayout>
</LinearLayout>
<LinearLayout>
//here would be another view, only shown when a button is clicked
<ViewFlipper>
</ViewFlipper>
</LinearLayout>
<RelativeLayout
</RelativeLayout>
</LinearLayout>
Use FrameLayout to show view over-lapping another view. You can keep the view as INVISIBLE or using GONE in the xml and then just make it visible when the Button is Clicked.
Yes...you should prepare it in xml and give it an id.then you can easily manage its visibility on button click using mLinearLayout.setVisibility(View.GONE); and mLinearLayout.setVisibility(View.VISIBLE); like:
Button mButton=(Button)findViewById(R.id.button);
LinearLayout ll=(LinearLayout)findViewById(R.id.frame_layout);
static int count=0;
mButton.setOnClick.... (new OnClick...()
public void onClick(){
count++;
if(count==1)
ll.setVisibility(View.VISIBLE);
else
{
count=0;
ll.setVisibility(View.GONE);
}
}
);
Here you have two options:
As you said pre-create layouts and set visibility to Visibility_Gone to layouts initially, not to be shown, set Visibitlity to View.Visible to display the layouts.
Another approach is to create views dynamically, and adding to parent on specified index, like to add on top of linearlayout use:
linearLayout.addView(view, 0);
If you want to show any view on button click then first put that view inside xml and make its visibility gone, and on button click make it visible. I have put imageview inside your code which visibility is set as gone so it wont show in layout.
<LinearLayout>
<LinearLayout>
</LinearLayout>
<LinearLayout>
//here would be another view, only shown when a button is clicked
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:visibility="gone" />
</LinearLayout>
<RelativeLayout
</RelativeLayout>
</LinearLayout>
For making image view visible,
imag1.seVisibility(View.VISIBLE);

Overlap views in android?

I have a linearlayout which have a textbox(multiline, nearly 5 lines) and image view. Is it possible to draw a image on textview(overlapping)?
Note: I have to specify the coordinates of the image, which are not static, and may be anywhere above text.
Something like this mockup:
I think it can be achieved using RelativeLayout.
<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="wrap_content" >
<TextView
android:id="#+id/Textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/Text2display"
android:textColor="#EEDCAA" />
<ImageView
android:id="#+id/choose_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="-46dp"
android:adjustViewBounds="true"
android:contentDescription="#string/description_logo"
android:src="#drawable/user2" />
</RelativeLayout>
By placing the TextView block above the ImageView, it ensures that the image view overlaps the TextView. Now, based on your requirements and position, use the following commands from the link :-
http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html
You can align left right, top and bottom. Use negative values to navigate the ImageView, if ur using align bottom and stuff.. This will make it to overlap. Please let me know if this was helpful
Is there any specific reason for Linear Layout?
You can do this easily using RelativeLayout . You can have an ImageView overlapping TextView Unless there is a specific reason for using LinearLayout .
If you really (really) need to use LinearLayout, you can subclass TextView and override onDraw to draw your image.
In all your xml files, should define the background color for it, it will solve the problem :
Add this android:background="#android:color/black" in to the View tag you defined.

Categories

Resources