How to make Image Buttons act like Radio buttons - java

I'm trying to make Image buttons act like Radio buttons. Let me explain. I have a basic layout containing an Image Button and a TextView, that I load with different images and texts.
XML layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/button_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#FFFFFF"/>
<TextView
android:id="#+id/text_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
Java method :
LinearLayout categoryLayout = view.findViewById(R.id.categories_layout);
for (int i = 0; i<categories.size(); i++) {
final String name = categories.get(i).name;
final int resource = categories.get(i).resource;
View v = getLayoutInflater().inflate(R.layout.choose_category, null);
final TextView text = v.findViewById(R.id.text_category);
text.setText(name);
ImageButton button = v.findViewById(R.id.button_category);
button.setImageResource(resource);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectedCategory = resource;
text.setTypeface(text.getTypeface(), Typeface.BOLD);
}
});
categoryLayout.addView(v);
}
Every time an image is clicked, the text is set bold to indicate which button was clicked. My problem is that I only want one button to be clickable at a time. I thought of, each time a button is clicked, reseting the appearance of all TextView, only leaving the last text that was clicked as bold. However, I don't know how to navigate through all layouts that have been generated.
I hope I was clear, thank you if you can help me !

That might not be the most efficient way to solve this problem, but it works. Every time the button is clicked, the icon selected is saved. Then, I use removeAllViews() method on the RadioGroup, and recreate the views. I just make sure the text below the previously selected icon is set bold.

Related

how to perform click action on multiple text views from another layout Android

I have a text view in another layout and i am generating multiple text views using that reference. The problem is that I am unable to set Tags to those text views for click listeners.
This is my code for generating multiple text views
final CircularLayout circularLayout = findViewById(R.id.circular_layout);
circularLayout.setCapacity(wordShuffled.length());
for (int i = 0; i < wordShuffled.length(); i++) {
text = (TextView)
LayoutInflater.from(this).inflate(R.layout.number_text_view, null);
text.setText("" + tmp[i]);
circularLayout.addView(text);
text.setTag(String.valueOf(i));
// m_ll.addView(text);
tv.setTag(String.valueOf(i));
m_ll.addView(text);
}
This is the layout which contains that text view
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textLay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorAccent"
android:textSize="22sp"
android:textStyle="bold" />
I want to have click actions on each text view
Create onClickListener out of for cycle, set it to generated textView. When you click textView, you should check tag and perform proper action.
final CircularLayout circularLayout = findViewById(R.id.circular_layout);
circularLayout.setCapacity(wordShuffled.length());
OnClickListener listener = new OnClickListener() {
//handle on click event
}
for (int i = 0; i < wordShuffled.length(); i++) {
...
textView.setTag(tag);
textView.setOnClickListener(listener);
...
}

Edit Text - Focus on Touch

I have created a note app.
In my app when i create a new note. By default, the cursor is at title edit text but I want to have a feature that if I touch anywhere on the screen the cursor should go to the description edit text. I have kept the description edit text in a separate linear layout which has a weight of 1.
Kindly find the images to get an idea.
In the given picture the area covered with the blue line I want to touch anywhere in that area and my focus should be directed to edit text where I have written the hint description
XML
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/scview"
android:layout_weight="1">
<EditText
android:id="#+id/body_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="18sp"
android:inputType="textMultiLine"
android:hint="Description"
android:backgroundTint="#android:color/transparent"/>.
</LinearLayout>
Use onClickListener() on parent layout of your xml like this
RelativeLayout parentRelativelayout=(RelativeLayout)findViewByRid(R.id.your_parent_layout_in_xml);
EditText edtDescription=(Edittext)findViewByRid(R.id.body_content);
parentRelativelayoutsetOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
edtDescription.requestFocus();
}
});
You need to override dispatchTouchEvent method and changes focus , as user touch screen , dispatchTouchEvent method will be fired.
#override Boolean dispatchTouchEvent(MotionEvent ev ) {
Toast.makeText(context ,"dispatchTouchEvent", Toast.LENGTH_SHORT).show()
return super.dispatchTouchEvent(ev)
}

How to create as many buttons as the number added within an edittext field?

How can I write a code whereby a user enters the number of buttons needed on a text field, these buttons then displays dynamically based on the number entered on the edit text field. Thanks
Read the count from the edit text and add the buttons like this inside a loop
Button button = new Button(this);
parent.addView(button);
Should be fairly simple to do this. Just add these attributes to your XML first.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.nero.myapplication.MainActivity">
<EditText
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Number of Buttons"
android:inputType="number"/>
<Button
android:id="#+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SUBMIT"/>
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
In your Edittext, make sure you have inputType to number so you can always expect to receive a whole number instead of string or anything else. I've also added a submit button which will listen to your request but you can change that however you like to trigger the function. Also you will need another layout/view where you will be displaying your buttons.
MainActivity
public class MainActivity extends AppCompatActivity {
EditText main;
Button submit;
LinearLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main = (EditText) findViewById(R.id.main);
layout = (LinearLayout) findViewById(R.id.layout);
submit = (Button)findViewById(R.id.btn_submit);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int noOfButtons = Integer.parseInt(main.getText().toString());
for(int i = 0; i < noOfButtons; i++){
Button button = new Button(MainActivity.this);
layout.addView(button);
}
}
});
}
}
Now you basically wire in all of your elements with your main activity class. Please note that I am using the old android studio version and that's why I have to declare the variable type (Button, Edittext, Layout) when I am wiring it in.
So you will simply create a onClickListener for your submit button which will then action the number of buttons the user has requested. It will then take the number in a loop and create a new button as needed.
Let me know if you need further info.

Customize Google SignInButton In Android

I want to customise Google SignIn Button In Android. Currently I have a very basic default layout by using following code.
<com.google.android.gms.common.SignInButton
android:id="#+id/sign_in_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
I am not satisfied with the current button layout. Is it possible to update the button text, background colour in the default button, Or Should I create full custom button layout?
Thank you.
ps: I am new to Android
Its very simple , you need to do this
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/common_signin_button_text_long" />
You can customize your button , but you will have to make sure you follow the guidelines.
Edit: Check out this library custom signin button
If you want to do it yourself you can create a linear layout and add images.
I think you have to do it pragmatically rather than xml something like this
public static void customizeGooglePlusButton(SignInButton signInButton) {
for (int i = 0; i < signInButton.getChildCount(); i++)
{
View v = signInButton.getChildAt(i);
if (v instanceof TextView)
{
TextView tv = (TextView) v;
tv.setText("My Text");
tv.setAllCaps(true);
tv.setCompoundDrawablesWithIntrinsicBounds( R.drawable.smiley, 0, 0, 0);
//here you can customize what you want
return;
}
}
}
another way but not that secure
TextView textView = (TextView) signInButton.getChildAt(0);
//Customize here
Basically, Google SignInButton is a FrameLayout you can customize it
as you want but need some dynamic tweaks.
XML part
adding custom background to the frame layout, make your own.
<com.google.android.gms.common.SignInButton
android:id="#+id/google_signIn"
android:layout_width="match_parent"
android:background="#drawable/google_bottun_bg"
android:layout_height="wrap_content"
android:layout_marginRight="5dp">
// google image logo
<ImageView android:layout_width="16dp"
android:layout_height="16dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="12dp"
app:srcCompat="#drawable/ic_gmail"/>
</com.google.android.gms.common.SignInButton>
inside FrameLayout we have a textView so you can redesign text view as needed
fun reDesignGoogleButton(signInButton: SignInButton, buttonText: String) {
for (i in 0 until signInButton.childCount) {
val v = signInButton.getChildAt(i)
if (v is TextView) {
v.text = buttonText //setup your text here
v.setBackgroundResource(android.R.color.transparent) //setting transparent color that will hide google image and white background
v.setTextColor(resources.getColor(R.color.white)) // text color here
v.typeface = Typeface.DEFAULT_BOLD // even typeface
return
}
}
}
Happy Coding
see the results
No need any library. The nice trick is to create your fully custom layout then place google btn in this layout and set width and height match parent for Google Sign in Button, then set Visibility for Google Btn = 0 and then in
googleSignInButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
googleLayoutCustom.setPressed(true);
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
});
So Google Sign In btn will be on top of all layouts and your custom layout will perform touch ripple on google btn click.
P.S sure you have to implement touch ripple effect with any colors you want in your drawable. Example:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/colorMain">
<item
android:id="#android:id/mask"
android:drawable="#android:color/white" />
</ripple>

Androids ExpandableListView - where to put the button listener for buttons that are children

I have been playing around a lot with the ExpandableListView and I cannot figure out where to add the button listeners for the button that will be the children in the view. I did manage to get a button listener working that uses getChildView() below, but it seems to be the same listener for all the buttons.
The best case scenario is that I would be able to implement the button listeners in the class that instantiates the ExpandableListAdapter class, and not have to put the listeners in the actual ExpandableListAdapter class. At this point I don't even know if that is possible
I have been experimenting with this tutorial/code: HERE
getChildView()
#Override
public View getChildView(int set_new, int child_position, boolean view, View view1, ViewGroup view_group1)
{
ChildHolder childHolder;
if (view1 == null)
{
view1 = LayoutInflater.from(info_context).inflate(R.layout.list_group_item_lv, null);
childHolder = new ChildHolder();
childHolder.section_btn = (Button)view1.findViewById(R.id.item_title);
view1.setTag(childHolder);
childHolder.section_btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Toast.makeText(info_context, "button pushed", Toast.LENGTH_SHORT).show();
}
});
}else {
childHolder = (ChildHolder) view1.getTag();
}
childHolder.section_btn.setText(children_collection.get(set_new).GroupItemCollection.get(child_position).section);
Typeface tf = Typeface.createFromAsset(info_context.getAssets(), "fonts/AGENCYR.TTF");
childHolder.section_btn.setTypeface(tf);
return view1;
}
Any help would be much appreciated. Thank you and I will be standing by.
If the buttons are in the ExpandableListView, their listener needs to be in the adapter.
I'm not sure the thrust of your question, but if you are asking how do you relate the button to the contents of the child row, I can answer that. :p
I'll assume a somewhat simple child row layout for demonstration purposes.
child_row.xml
<?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:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="#+id/ListItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:paddingLeft="7dip"
android:paddingRight="7dip"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/ListItem2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right|center_vertical"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Then, to get the contents of the row when your button is pressed, you use the button to backtrack to the parent vieew and then get the necessary child views and their contents:
childHolder.section_btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
LinearLayout ll = (LinearLayout) v.getParent(); // get the view containing the button
TextView tv1 = (TextView) ll.findViewById(R.id.ListItem1); // get the reference to the first widget
TextView tv2 = (TextView) ll.findViewById(R.id.ListItem2); // get the reference to the second widget
String text1 = tv1.getText.toString(); // Get the contents of the first widget to a string
String text2 = tv2.getText.toString(); // Get the contents of the second widget to a string
}
});
If this isn't what you were looking for clarify your question and I'll take another shot at it.

Categories