I have two layouts. I want to keep one layout gone when the activity is loaded and it should be visible onClick of another layout. so I have added this code OnClickListener of the layout.
additionalContactFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(linearLayoutFrom.getVisibility() == View.GONE){
linearLayoutFrom.setVisibility(View.VISIBLE);
}else{
linearLayoutFrom.setVisibility(View.GONE);
}
}
});
And have set visibility of the layout gone in xml file..
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/LinearLayoutAdditionalContactFrom">
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/imageView13"
android:layout_marginLeft="20dp"
android:background="#drawable/ic_person_black_48dp"
android:layout_marginTop="05dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_marginRight="10dp"
android:drawableRight="#drawable/ic_expand_more_black_24dp"
android:text="Additional contact (optional)"
android:cursorVisible="false"
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginRight="50dp"
android:layout_gravity="center"
android:visibility="gone"
android:id="#+id/LinearLayoutFrom">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText2"
android:layout_weight="1"
android:hint="Name"
android:layout_gravity="center"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText3"
android:layout_weight="1"
android:hint="Phone"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="OR"
android:id="#+id/textView19"
android:layout_gravity="center" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/textView13"
android:layout_marginLeft="48dp"
android:hint="Input if you're not receiver" />
</LinearLayout>
Unable to understand whats going wrong.. The listener is not getting called at all.. Please help..
Your problem is that your EditText is capturing the click event when you click on it. If you click somewhere else in the LinearLayout it should work.
You can replace the EditText with a TextView if you don't need the user to edit the content.
Change
linearLayoutFrom.setVisibility(View.VISIBLE);
To
findViewById(R.id.YOURLAYOUTID).setVisibility(View.VISIBLE);
I think you can't access to local vars in sub methods
It Seems That Layout Visibility is Already set to GONE, so Onlick listener is not working on hidden View and it should not work too.
INVISBLE means you are trying to add a listener to a view which is not there. You can add the listener to a visible view only.
WORKAROUND
1) Try to make a dummy view which is visible but having the same color as background.
2) Try to set the listener for parent and check the position (whether the position does
belongs to INVISIBLE view).
Please try to set the onClickListener to the EditText and to the ImageView and no to the LinearLayout
The problem is that the Handler of the EditText is most important that the LinearLayout handler.
Almost you can try to make a break point to the OnClick and see what is happend
This is an example to explain you how to do that:
in MainActivity Class:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
private LinearLayout linearLayoutFrom;
private LinearLayout additionalContactFrom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
additionalContactFrom = (LinearLayout) findViewById(R.id.LinearLayoutAdditionalContactFrom);
linearLayoutFrom = (LinearLayout) findViewById(R.id.LinearLayoutFrom);
linearLayoutFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"linearLayoutFrom clicked!!!", Toast.LENGTH_SHORT)
.show();
if (additionalContactFrom.getVisibility() == View.GONE) {
additionalContactFrom.setVisibility(View.VISIBLE);
} else {
additionalContactFrom.setVisibility(View.GONE);
}
}
});
additionalContactFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"additionalContactFrom clicked!!!", Toast.LENGTH_SHORT)
.show();
if (linearLayoutFrom.getVisibility() == View.GONE) {
linearLayoutFrom.setVisibility(View.VISIBLE);
} else {
linearLayoutFrom.setVisibility(View.GONE);
}
}
});
}
}
in xml file:
<FrameLayout 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:orientation="vertical" >
<LinearLayout
android:id="#+id/LinearLayoutAdditionalContactFrom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_dark"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayoutFrom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="60dp"
android:layout_marginRight="50dp"
android:background="#android:color/holo_green_light"
android:orientation="vertical"
android:visibility="gone" >
</LinearLayout>
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="48dp"
android:hint="Input if you're not receiver"
android:textAppearance="?android:attr/textAppearanceSmall" />
</FrameLayout>
This is very important that when you want add some view (for example
add a linearlayout to another linerlayout). you should use framelayout or
relativelayout(do not use linearlayout) for do that.
Related
<?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"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="#+id/away"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="#string/away"
android:gravity="center"
android:textSize="20sp"/>
<TextView
android:id="#+id/score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:gravity="center"
android:text="#string/_0"
android:textSize="20sp" />
<Button
android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="50dp"
android:onClick="score1"
android:text="#string/score" />
<TextView
android:id="#+id/home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:text="#string/home"
android:textSize="20sp"
android:gravity="center"/>
<TextView
android:id="#+id/score2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginBottom="50dp"
android:text="#string/_0"
android:textSize="20sp" />
<Button
android:id="#+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="score2"
android:text="#string/score" />
<Button
android:id="#+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/reset"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:textColor="#ff1100"/>
</LinearLayout>
I searched it on multiple websites but I still can't the answer. I also tried using the__setText("")it
didn't work as well as the the if() method. I have also tried the intent method also doesn't seem to
work, and many other codes , please help me because I am stuck at this bit , I know that it is simple but
I just can't find it. Thank you
The following codes will set the TextView score to blank when Button one is clicked
First approach
To use the android:onClick="score1" in your XML
Add this to your activity
TextView mTvScore1 = (TextView) findViewById(R.id.score)
public void score1(View v) {
mTvScore1.setText("");
}
Second approach
Button mBtn = (Button) findViewById(R.id.one)
TextView mTvScore1 = (TextView) findViewById(R.id.score)
mBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTvScore1.setText("");
}
});
Activity code
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView mTvScore1 = (TextView) findViewById(R.id.score);
Button mBtn = (Button) findViewById(R.id.one);
mBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
score1(v);
}
});
}
public void score1(View v) {
mTvScore1.setText("");
}
}
I'm still new to the Android and I want to add a CardView to the activity each time a button is clicked. The CardView has text on it and a background image. I already have the XML file that can add this, but because I want to be able to add more than one, I can't use <include.>. The first image is when the button is clicked once and the second is when the button is clicked 3 times. I already have the onClick for the TextView that says "Click To Add Block" and the XML for the CardView, but I can't make it so that you can add them and change the text in the TextView in each and every one of them. I also can't seem to find a way to programmatically add an onClick listener to the programmatically created CardView. Later down the line, I would also like to be able to delete the CardView from a click of a button too.
Here is the CardView XML file (Before it was inside a Relative Layout)
<androidx.cardview.widget.CardView
android:id="#+id/cardviewClassesBlock1"
android:layout_width="330dp"
android:layout_height="75dp"
android:layout_marginTop="90dp"
android:layout_centerHorizontal="true"
app:cardCornerRadius="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/ic_launcher_background">
<TextView
android:id="#+id/textviewClassesBlock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="3dp"
android:textSize="22sp"
android:fontFamily="#font/amiko_semibold"
android:textColor="#color/white"
android:text="Block A"/>
<ImageView
android:layout_width="60dp"
android:layout_height="6dp"
android:layout_marginStart="10dp"
android:layout_below="#+id/textviewClassesBlock1"
android:background="#drawable/rounded_corner_edittext" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="5dp"
android:textColor="#color/white"
android:text="P - 0 | T - 0 | A - 0"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
I have created a sample project for You.
First You have to create a layout for Your CardView. In res/layout create card_base.xml. In this layout add:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardviewClassesBlock1"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_centerHorizontal="true"
app:cardCornerRadius="10dp"
android:layout_margin="10dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/ic_launcher_background"
>
<TextView
android:id="#+id/textviewClassesBlock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="3dp"
android:text="Block A"
android:textSize="22sp"
/>
<ImageView
android:layout_width="60dp"
android:layout_height="6dp"
android:layout_below="#+id/textviewClassesBlock1"
android:layout_marginStart="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="5dp"
android:text="P - 0 | T - 0 | A - 0"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
This is basically Your CardView with small changes.
Next, in Your activity_main.xml add this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
>
<Button
android:id="#+id/butAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add New Card"
/>
<Button
android:id="#+id/butDoSth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Do something"
/>
<androidx.appcompat.widget.LinearLayoutCompat
android:id="#+id/cards"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<include
android:id="#+id/includedLayoutFirst"
layout="#layout/card_base"
/>
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
This is a starter (very simple) look of Your app. It has one button and one CardView which is already inserted.
Now in Your MainActivity.java paste this:
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.cardview.widget.CardView;
public class MainActivity extends AppCompatActivity
{
private int starter = 66; //ASCII code for `B`
LinearLayoutCompat cards;
Button buttonAdd;
Button buttonDoSth;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cards = findViewById(R.id.cards);
buttonAdd = findViewById(R.id.butAdd);
buttonDoSth = findViewById(R.id.butDoSth);
buttonAdd.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
CardView newCard = new CardView(MainActivity.this);
getLayoutInflater().inflate(R.layout.card_base, newCard);
TextView t = newCard.findViewById(R.id.textviewClassesBlock1);
String current = Character.toString((char) starter++);
t.setText("Block " + current);
newCard.setTag(current); //
cards.addView(newCard);
}
});
buttonDoSth.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
findBlockAndDoSomething("B");
}
});
}
private void findBlockAndDoSomething(String name)
{
Log.d("MyTAG", "CLICK");
for (int i = 0; i < cards.getChildCount(); i++)
{
CardView selected = (CardView) cards.getChildAt(i);
if (selected.getTag() != null && selected.getTag().toString().equals(name))
{
// do something. E.g change block name
TextView textViewClassesBlock1 = selected.findViewById(R.id.textviewClassesBlock1);
textViewClassesBlock1.setText("Block XXX");
return;
}
}
}
}
Result (starter code and with adding new CardView):
I'm working on a music player application, and I've created a FrameLayout which works as a "card," displaying information about each song (album art, title, artist, etc.) I'm trying to add checkboxes in this card, which I plan to customize to create upvote and downvote buttons. However, the checkboxes within the card do not respond to clicks. If I draw a checkbox in the main activity of the application (where I'm drawing the cards), it works fine.
Here is the onDraw() method of the card view ("MusicCardView"):
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//Draw the voting controls
try {
CheckBox upvoteButton = new CheckBox(getContext());
upvoteButton.setX(20);
upvoteButton.setTop(-60);
upvoteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Is the button now checked?
Log.d("MainActivity", "upvote clicked");
}
});
CheckBox downvoteButton = new CheckBox(getContext());
downvoteButton.setX(20);
downvoteButton.setTop(-150);
downvoteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Is the button now checked?
Log.d("MainActivity", "downvote clicked");
}
});
upvoteButton.draw(canvas);
downvoteButton.draw(canvas);
} catch (Exception e) {
Log.e("MusicCardView", "exception", e );
}
}
The xml of the main activity ("PartyActivity"):
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="#+id/main_activity_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.musique.PartyActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/main_linear_view"
android:layout_marginBottom="56dp">
<com.musique.MusicCardView
android:layout_width="match_parent"
android:id="#+id/now_playing_view"
android:layout_height="80dp"
android:background="#ccc"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
app:exampleColor="#000000"
app:songTitle="Money"
app:artist="Pink Floyd"
app:album="The Dark Side of the Moon"
app:exampleDimension="18sp"
app:exampleDrawable="#drawable/darkside"
app:exampleString="example" />
<Space
android:layout_width="match_parent"
android:layout_height="80dp"
android:id="#+id/controller_space"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/query"
android:layout_gravity="center"
android:layout_marginTop="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/searchBtn"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Search"
android:background="#5eca99"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvData"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:animateLayoutChanges="true"
android:id="#+id/main_scroll_view">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/scroll_child">
</LinearLayout>
</ScrollView>
</LinearLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/navigation" />
</android.support.constraint.ConstraintLayout>
And an example of a MusicCardView being the draw in PartyActivity
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
MusicCardView v = (MusicCardView) vi.inflate(R.layout.music_card_template, null);
v.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, 210));
LinearLayout linearLayout = findViewById(R.id.scroll_child);
v.setSongAttributes(song);
new RetrieveArtTask(v).execute(song);
Log.d("PartyActivity", "SongAttributes Set");
linearLayout.addView(v);
I've had a look at this question and this question, but had no luck with the solutions posted, and would greatly appreciate any advice you might have.
If you draw the controls directly to the canvas they are basically just bitmaps, you need to add them to the view hierarchy (activity/fragment layout) for them to receive touch events.
As a side note, it's a big no-no to instantiate things in onDraw, especially views.
I would like to ask on how to achieve to wrap a content in alertdialog?
Because the current output of my dialog has an excess white field.
Hope someone can help me to understand this problem thanks.
Here is my activity_dialog.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:minWidth="10dp"
android:minHeight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context="com.bloxofcode.toggle.MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="50sp"
android:background="#color/colorHeaderDialog"
android:textColor="#android:color/white"
android:text="#string/select_gender"
android:padding="15dp"
android:gravity="center_horizontal|left"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:padding="15dp"
android:text="Sample"
android:id="#+id/editText" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ToggleButton
android:text="ToggleButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/toggleButtonMale"
android:textOn=""
android:textOff=""
android:focusable="false"
android:background="#drawable/check_male"
android:layout_weight="1" />
<ToggleButton
android:text="ToggleButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/toggleButtonFemale"
android:textOn=""
android:textOff=""
android:focusable="false"
android:background="#drawable/check_male"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<Button
android:id="#+id/btnCancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cancel"
android:textSize="20dp"
android:padding="30dp"
android:textColor="#android:color/white"
android:layout_weight="1"/>
<Button
android:id="#+id/btnAccept"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Accept"
android:textSize="20dp"
android:padding="30dp"
android:background="#color/colorDialogOK"
android:textColor="#android:color/white"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Here is some of my implementation in the MainActivity.java:
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
View mView = getLayoutInflater().inflate(R.layout.activity_dialog, null);
.....
mBuilder.setView(mView);
final AlertDialog dialog = mBuilder.create();
dialog.show();
Try Creating a dialog like this. this works perfect and then right away your dialog logic is seperated from you activity logic
public class CustomDialog extends Dialog implements
android.view.View.OnClickListener {
public Activity c;
public CustomDialog d;
public Button yes, no;
public CustomDialog(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_layout);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//dosomething
}
});
}
}
Hope it is not correct to wrap the content of dialog as it might disrupt the view in tablets and larger devices.
Anyhow, basing upon your requirement.. hope this link works
AlertDialog with custom view: Resize to wrap the view's content
I want the code in the Java file to set the background color of whatever button was clicked, and keep it, even after you let go of the button. Thanks in advance
I don't really get the formatting on here. I hope it's clear what belongs where.
Java:
package mika.actual;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class accordion extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.accordian);
Button btnProfile = (Button) findViewById(R.id.btnProfile);
Button btnSettings = (Button) findViewById(R.id.btnSettings);
Button btnPrivacy = (Button) findViewById(R.id.btnPrivacy);
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.GONE);
View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.GONE);
View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.GONE);
btnProfile.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// DO STUFF
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.VISIBLE);
View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.GONE);
View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.GONE);
}
});
btnSettings.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// DO STUFF
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.GONE);
View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.VISIBLE);
View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.GONE);
}
});
btnPrivacy.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// DO STUFF
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.GONE);
View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.GONE);
View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.VISIBLE);
}
});
}
}
XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF"
android:orientation="vertical">
<Button
android:id="#+id/btnProfile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Profile"
android:textColor="#FFFFFFFF" />
<LinearLayout
android:id="#+id/panelProfile"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF">
<LinearLayout
android:id="#+id/panelProfile1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="#+id/strName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<EditText
android:id="#+id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/panelProfile2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="#+id/strSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Surname" />
<EditText
android:id="#+id/txtSurname"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="#+id/btnSettings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Settings"
android:textColor="#FFFFFFFF" />
<LinearLayout
android:id="#+id/panelSettings"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/panelSettings1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="#+id/strMail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="e-mail" />
<EditText
android:id="#+id/txtMail"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/panelSettings2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="#+id/strPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone" />
<EditText
android:id="#+id/txtPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="#+id/btnPrivacy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Privacy"
android:textColor="#FFFFFFFF" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/panelPrivacy"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<CheckBox
android:id="#+id/checkFacebook"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Facebook"
android:textColor="#ff355689">
</CheckBox>
<CheckBox
android:id="#+id/checkLinkedIn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LinkedIn"
android:textColor="#ff355689">
</CheckBox>
<CheckBox
android:id="#+id/checkTwitter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Twitter"
android:textColor="#ff355689">
</CheckBox>
</LinearLayout>
</ScrollView>
</LinearLayout>
If I understood correctly, what you want to do is, when clicking a button and expanding its sub-menu, change the background of that button until it is clicked again. You can do this with a Tag on the Button:
btnSettings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// DO STUFF
//if button is not selected, change background and tag the View as selected
if(v.getTag() == null || v.getTag().equals("not_selected")) {
v.setBackgroundColor(Color.RED);
v.setTag("selected");
//show sub-menu
}
//if button is already selected, reset background and tag the View as not selected
else{
v.setBackgroundResource(android.R.drawable.btn_default);
v.setTag("not_selected");
//hide sub menu
}
}
});
Notice that v.setBackgroundResource(android.R.drawable.btn_default); is resetting the Button background to an android drawable. You may want to change this to a custom drawable or color.
You could have used an ExpandableListView to implement this kind of menu.
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
view.setBackgroundColor(ContextCompat.getColor(context, R.color.presed_color));
} else if (event.getAction() == MotionEvent.ACTION_UP) {
view.setBackgroundColor(ContextCompat.getColor(context, R.color.unpresed_color));
}
return false;
}
});