Showing button part by part in Android - android

Hi
I am new in android.
I have many Buttons in view in android and i want to show them part by part by clicking another button.

Set button visibility to GONE (button will be completely "removed" -- the buttons space will be available for another widgets) or INVISIBLE (button will became "transparent" -- its space will not be available for another widgets):
View b = findViewById(R.id.button);
b.setVisibility(View.GONE);
or in xml:
<Button ... android:visibility="gone"/>
Ref By: LINK

use android:visibility="visible" or android:visibility="gone" or android:visibility="invisible"
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="91dp"
android:layout_toLeftOf="#+id/textView1"
android:text="Button"
android:visibility="visible" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_alignLeft="#+id/textView1"
android:text="Button"
android:visibility="invisible" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_toRightOf="#+id/button2"
android:text="Button"
android:visibility="invisible" />

let say you have 2 buttons : in xml write this:Make button 2 invisible like below
<Button android:id="#+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 1"/>
<Button android:id="#+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="button 2"
android:layout_below="#+id/btn1"
android:visibility="invisible"/>
In your oncreate method write this: when you click on button1 , button2 will appear.
Button btn1 = (Button)findViewById(R.id.btn1);
final Button btn2 = (Button)findViewById(R.id.btn2);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn2.setVisibility(View.VISIBLE);
}
});

<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="91dp"
android:layout_toLeftOf="#+id/textView1"
android:text="Button"
android:visibility="gone" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_alignLeft="#+id/textView1"
android:text="Button"
android:visibility="gone" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_toRightOf="#+id/button2"
android:text="Button"
android:visibility="gone" />
First make the above xml layout having all your buttons and set them to GONE because the buttons will not occupy space in the layout by default so saves you space and time at initialization.
Now in your code on click event just set the visibility of these buttons to VISIBLE.
Steps for changing the visibility
HashMap map=new HashMap();
map.put(R.id.button1,new Integer[]{R.id.button2,R.id.button3,R.id.button4});
public void onClick(View v){
Integer[] buttonsToShow=map.get(R.id.v.getId());
if(buttonsToShow!=null)
for(int button:buttonsToShow){
findViewById(button).setVisibility(View.VISIBLE);
}
}

I wrote following code and solved my problem. maybe it might be useful to anyone
next = (Button)findViewById(R.id.next);
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
if(i<movie_list.length-3)
{
movie_list[i].setVisibility(View.GONE);
movie_list[i+3].setVisibility(View.VISIBLE);
i++;
}else{
Toast.makeText(getApplicationContext(), "That is the end of the buttons ", Toast.LENGTH_SHORT).show();
}
}
});

Related

Dialog smaller in SDK 23 than SDK 22

I have an app that uses two buttons and a number picker. Everything works fine when running an emulator with SDK 22 but when running SDK 23 the dialog is significantly smaller and does not include the leftmost button.
Here is the xml for the dialog:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<NumberPicker
android:id="#+id/numberPicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/numberPicker1"
android:layout_marginLeft="20dp"
android:layout_marginTop="98dp"
android:layout_toRightOf="#+id/numberPicker1"
android:text="Cancel" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_marginRight="16dp"
android:layout_toLeftOf="#+id/numberPicker1"
android:text="Set" />
</RelativeLayout>
Here is the code I use to initialize the dialog:
final Dialog d = new Dialog(MainActivity.this);
d.setTitle("Delay");
d.setContentView(R.layout.dialog);
Button b1 = (Button) d.findViewById(R.id.button1);
Button b2 = (Button) d.findViewById(R.id.button2);
final NumberPicker np = (NumberPicker) d.findViewById(R.id.numberPicker1);
np.setMaxValue(100);
np.setMinValue(0);
np.setWrapSelectorWheel(false);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
changeDelay(np.getValue(), user);
d.dismiss();
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
d.dismiss();
}
});
d.show();
Try the layout below:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<NumberPicker
android:id="#+id/numberPicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp"
android:layout_marginBottom="98dp"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/numberPicker1"
android:layout_alignParentRight="true"
android:text="Cancel" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/numberPicker1"
android:layout_alignParentLeft="true"
android:text="Set" />
</RelativeLayout>
Some problems, are:
android:layout_alignBottom will cause the RelativeLayout to expand until the bottom of the screen. This is not good for dialog. You can find details here
You defined some huge margins. Your dialog in landscape mode is not displayed properly... Some contents become hidden.
But I'm not sure why it was working fine on API 22 and not on API 23

Change values (colors) of TextView with a button click

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);
}
}););

Updating a Textview with a Button click (both in a custom Dialog)

I want to create a custom keyboard inside a custom dialog.
It has a Textview on top and 12 buttons bellow (it's a numerical keyboard)
What I want is: when the buttons are pressed they update the Textview values.
I don't know the right place to put the "OnClickListener" and how to update the Textview...
Here is the keyboard layout:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView
android:id="#+id/valor"
android:layout_width="300dp"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<Button
android:id="#+id/btn7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7"/>
<Button
android:id="#+id/btn8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8"/>
<Button
android:id="#+id/btn9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9"/>
</TableRow>
<TableRow>
<Button
android:id="#+id/btn4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4"/>
<Button
android:id="#+id/btn5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5"/>
<Button
android:id="#+id/btn6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6"/>
</TableRow>
<TableRow>
<Button
android:id="#+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"/>
<Button
android:id="#+id/btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2"/>
<Button
android:id="#+id/btn3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3"/>
</TableRow>
<TableRow>
<Button
android:id="#+id/btn0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0"/>
<Button
android:id="#+id/btnP"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="."/>
<Button
android:id="#+id/btnOk"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Ok"/>
</TableRow>
And here is the MainActivity:
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button teclado = (Button)findViewById(R.id.teclado);
teclado.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
showCustomDialog();
}
});
}
public void showCustomDialog()
{
final Dialog dialog = new Dialog(this);
dialog.getWindow();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.teclado);
dialog.show();
final TextView valor = (TextView) dialog.findViewById(R.id.valor);
Button btn7 = (Button)dialog.findViewById(R.id.teclado);
btn7.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
valor.setText("Test");
}
});
}
}
Thanks in advance!!
Sorry, I found this implementation strange.
I would suggest using DialogFragment, and you can encapsulate everything in there.
http://android-developers.blogspot.ca/2012/05/using-dialogfragments.html
You will be able to use the normal setOnClickListener in that fragment.
Button btn7 = (Button)dialog.findViewById(R.id.btn7); **"why R.id.teclado?"**
btn7.setOnClickListener(this)
And your fragment can implement OnClickListener with switch
#Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btn7:
valor.setText("asdf");
break;
case R.id.btn8:
...
break;
....
}
// TODO Auto-generated method stub
}
Ok I want to give some basic idea regarding this. you can modify this according to your requirement.
Give your layout an id. and use layoutinflater to inflate your xml in dialog.
If you want your xml layout in your dialog box you you can do it like this:
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.your_layout_id_here, (ViewGroup) getCurrentFocus());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);
builder.show();
Put above code in Button's onclicklistener. And check Button's view through Switch case like case 1: "this button id" then insert this number in Text view.
Hope this help you
R.id.teclado is not in the dialog layout
Change:
Button btn7 = (Button)dialog.findViewById(R.id.teclado);
To:
Button btn7 = (Button)dialog.findViewById(R.id.btn7);

Android - A button with Image + background + text

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
}
});

PopUpView or AlertDialog, with ImageButton matrix

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();
}
});

Categories

Resources