I have a button, the button has a background and a src Image icon.
Now I want to have a text on the bottom, inside my button.
How can I achieve this?
I cannot use ImageButton as it doesn't handle text.
Also I cannot use Button as it doesn't handle src Image.
Try this..
Use android:drawableTop="#drawable/ic_launcher"
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/ic_launcher"
android:text="Button" />
Or
<RelativeLayout
android:id="#+id/btn_lay"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="5dp"
android:gravity="center"
android:text="Cal" />
</RelativeLayout>
ClickListener
RelativeLayout btn_lay = (RelativeLayout) findViewById(R.id.btn_lay);
btn_lay.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Do Something
}
});
Related
Here's the layout of the cardview in context:
<android.support.v7.widget.CardView
android:id="#+id/card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_marginTop="-25dp"
android:clickable="true"
app:cardUseCompatPadding="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardBackgroundColor="#color/md_white_1000"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/icon"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:maxLines="4"
android:textColor="#color/md_teal_900"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content"
android:focusable="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:clickable="true"
android:id="#+id/icon"
android:src="#mipmap/ic_download" />
</RelativeLayout>
</android.support.v7.widget.CardView>
I want only the icon (imageview) to be clickable. This cardview is part of a recyclerview, and I can only make the whole card responsive to click, whereas I want only the imageview to be clickable.
You can do #Michele Lacorte's answer with onClick() method and then
ImageView image = (ImageView) findViewById(R.id.icon);
image.setOnClickListener(this);
Set OnClickListener to ImageView, here an example:
ImageView image = (ImageView) findViewById(R.id.icon);
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do operation
}
});
EDIT:
If you using onClick() method:
#Override
public void onClick(View v) {
switch(v.getId()){
//Add this case to your switch
case R.id.icon:
//Do operation
break;
}
}
And as #vspallas says do not forget to add Listener
image.setOnClickListener(this);
So I'm trying to make this EditText to go from a textColor to another when I hit that specific button (for instance: button1 change the textcolor to BLUE, button2 change it to GREEN etc.)
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/test"
android:id="#+id/test"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#color/orange_test"
android:layout_toLeftOf="#+id/colorBtn"
android:layout_alignBottom="#+id/colorBtn"
android:textSize="25sp"
android:gravity="center_vertical|center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/colorBtn"
android:id="#+id/colorBtn"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="changeColor"/>
How should I have to proceed? :\
Duplicate your button to set more than one color
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/colorBtn"
android:id="#+id/colorBtn"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="changeColor"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/colorBtnRed"
android:id="#+id/colorBtnRed"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="changeColor"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/colorBtnGreen"
android:id="#+id/colorBtnGreen"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="changeColor"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/colorBtnBlue"
android:id="#+id/colorBtnBlue"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="changeColor"
/>
In your changeColor, check the button id. If it is the colorButtonBlue id, then set the text color of your EditText to blue. If it is colorButtonRed, ther set to red abd so on...
public void changeColor(View v)
{
switch(v.getId())
{
case R.id.colorBtnRed:
edtTest.setTextColor(Color.RED);
break;
case R.id.colorBtnGreen:
edtTest.setTextColor(Color.GREEN);
break;
case R.id.colorBtnBlue:
edtTest.setTextColor(Color.BLUE);
break;
default:
edtTest.setTextColor(Color.BLACK);
}
}
[EDIT]
Of course, in your Declaration section, do this:
EditText edtTest = null;
And in your onCreate do something like this (after the super and setContentVieew lines):
edtTest = (EditText) findViewById(R.id.test);
You mean something like this? -
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editText1.setTextColor(Color.GREEN);
}
}););
I have a problem with a custom layout.
When running it on emulators, the popup adjusts itself to the size of my custom layout, yet when I run it on my Atrix, this happens:
As you can see, the layout is not centered.
Now, this is my custom layout:
<TextView
android:id="#+id/txtVersus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:padding="10dp"
android:text="#string/popup_unlock"
android:textColor="#FFFFFFFF"
android:textSize="20sp"
android:visibility="visible" />
<ImageView
android:id="#+id/imgPopup"
android:layout_width="#dimen/imagen_config_width"
android:layout_height="#dimen/imagen_config_height"
android:layout_gravity="center"
android:background="#drawable/char_cersei"/>
<net.rahl.rahlutils.CustomFontTextView
android:id="#+id/txtPopupName"
style="#style/ConfigText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="3dp"/>
<net.rahl.rahlutils.CustomFontTextView
android:id="#+id/txtPopupCasa"
style="#style/ConfigText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="3dp"/>
<Button
android:id="#+id/btnCerrarPopup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:text="#string/ok" />
And this is my part of the code where I set and show it:
final Dialog dialog = new Dialog(con);
dialog.setTitle("Felicidades!");
dialog.setContentView(R.layout.unlockdialog);
((ImageView) dialog.findViewById(R.id.imgPopup)).setBackgroundResource(con.getResources().getIdentifier("char_" + name.toLowerCase(), "drawable", con.getPackageName()));
((TextView) dialog.findViewById(R.id.txtPopupName)).setText(name);
((TextView) dialog.findViewById(R.id.txtPopupCasa)).setText(casa);
((Button) dialog.findViewById(R.id.btnCerrarPopup)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
dialog.cancel();
}
});
dialog.setCanceledOnTouchOutside(false);
dialog.show();
Do you know how to fix this?
How can I handle in my activity layout long text from which I donĀ“t know how long it is?
I have this text in a textview and when it is too long, it overlays my button.
Are you talking about a Textview or Button? If its a Textview ,You can use a marque textview like below.It will scroll automatically so that all the text will be shown
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ellipsize="marquee"
android:lines="1"
android:fadingEdge="horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:text="Your Looooooooooooooooooooooooooooooooooooooooong text!"
android:textColor="#000"
android:textSize="15dp" />
and in your code,
textview.setSelected(true);
textview.setEllipsize(TruncateAt.MARQUEE);
textview.setHorizontallyScrolling(true);
textview.setSingleLine(true);
textview.setLines(1);
If you want an expandable Textview, do something like this.It have a button to expand/collapse textview.
In xml,
<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=".MainActivity" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="sdfsdfsdfsfasfdsfgdfgdfsgsdfgsdfgsdfgsdgsdgdsgdsgsdfgdsgdsfgsdfgsdgsdfgsdfgdsfgsdgdsgfsdgfsdgsdfgdsgdsfgdsfgdsfgsdghjghjghjghjghjfg"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeLayout1"
android:text="Button" />
</RelativeLayout>
and in code,
boolean checkflag = true;// declare it as public
final RelativeLayout rl=(RelativeLayout)findViewById(R.id.relativeLayout1);
Button b=(Button)findViewById(R.id.button1);
final TextView text=(TextView)findViewById(R.id.textView1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(checkflag==true)
{
text.setSingleLine(true);
checkflag=false;
}
else
{
text.setSingleLine(false);
checkflag=true;
}
}
});
I need to create a dialog that contains a matrix of simple square ImageButtons (like icons), in unknown number, and a Cancel button. The dialog should be inflatable programmatically and scrollable.
I really am confused: should I create a custom layout and apply it to AlertDialog? And how could I intercept the clicks? Should I use PopUpWiew? Should I make it as Activity or not?
And if I create a new Activity... should I use a Runnable?
Please don't provide me a complete source, I just need to understand what is the correct direction to go for this need.
Thank you in advance.
You can use a Dialog and set your layout file to it. A dialog is something like a mini activity which can be called onto an actual activity and dismissed at will.
Let this be the custom layout file - customLayout.xml
<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"
android:background="#drawable/bg"
tools:context=".AutoMode" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_above="#+id/relativeLayout2"
android:layout_centerHorizontal="true" >
<Button
android:id="#+id/button1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/blue_gradient"
android:layout_alignRight="#+id/relativeLayout1"
android:layout_alignTop="#+id/relativeLayout1"/>
<Button
android:id="#+id/button2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/black_gradient"
android:layout_alignTop="#+id/relativeLayout1"
android:layout_toRightOf="#+id/button1"/>
<Button
android:id="#+id/button3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:layout_toRightOf="#+id/button2"
android:background="#drawable/red_gradient" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
<Button
android:id="#+id/button4"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/yellow_gradient"
android:layout_alignRight="#+id/relativeLayout2"
android:layout_alignTop="#+id/relativeLayout2" />
<Button
android:id="#+id/button5"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/metallic_gradient"
android:layout_alignTop="#+id/relativeLayout2"
android:layout_toRightOf="#+id/button4" />
<Button
android:id="#+id/button6"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/purple_gradient"
android:layout_alignTop="#+id/relativeLayout2"
android:layout_toRightOf="#+id/button5"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_below="#+id/relativeLayout2"
android:layout_centerHorizontal="true" >
<Button
android:id="#+id/button7"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/green_gradient"
android:layout_alignRight="#+id/relativeLayout3"
android:layout_alignTop="#+id/relativeLayout3" />
<Button
android:id="#+id/button8"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/orange_gradient"
android:layout_alignTop="#+id/relativeLayout3"
android:layout_toRightOf="#+id/button7" />
<Button
android:id="#+id/button9"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/violet_gradient"
android:layout_alignTop="#+id/relativeLayout3"
android:layout_toRightOf="#+id/button8" />
</RelativeLayout>
Here's how you can set this layout to a dialog box in the activity where you want to call it:
final Dialog customDialog = new Dialog(this);
customDialog.setTitle("Matrix");
disclaimer.setContentView(R.layout.customLayout);
Button b1 = (Button) customDialog.findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Do whatever you want when button1 is clicked
}
});
.
.//same for other buttons
.
Button b9 = (Button) customDialog.findViewById(R.id.button9)
b9.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Do whatever you want when button9 is clicked
}
});
Button close = (Button) customDialog.findViewById(R.id.dismiss);
customDialog.show();
close.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
disclaimer.dismiss();
}
});