Make a selected state for textview (highlighted) - java

So currently, I am trying to create a selected state for three textviews
Currently, for each of the textviews ( H M S) the text is red:
However, when tap the H M or S I want it to turn another color--white.
So I tried to follow this:
Android - Textview change color on changing of state
and I did this (selected_text.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:color="#ffd10011"/> <!-- selected -->
<item android:color="#color/red_highlight"/> <!-- default -->
</selector>
and applied that:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Hours"
android:textSize="30sp"
android:textColor="#color/selected_text"
android:id="#+id/hourtext"
android:layout_marginLeft="45dp"
android:layout_alignTop="#+id/minutetext"
android:layout_alignLeft="#+id/seekArc"
android:layout_alignStart="#+id/seekArc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Minutes"
android:textSize="30dp"
android:textColor="#color/selected_text"
android:id="#+id/minutetext"
android:layout_below="#+id/seekArc"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Second"
android:textSize="30dp"
android:textColor="#color/selected_text"
android:id="#+id/secondtext"
android:layout_alignTop="#+id/minutetext"
android:layout_alignRight="#+id/seekArc"
android:layout_alignEnd="#+id/seekArc"
android:layout_marginRight="43dp" />
However, the textviews do not change color after I click on them.
How do I fix this?
Also,
I was wondering if it is better to implement this in java code since I need to perform a different function after each textview is clicked/highlighted. If so, how can that be implemented?

You can do this programatically like this:
findViewById(R.id.txt).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView textView = (TextView) v;
if (textView.isSelected()) {
textView.setTextColor(Color.RED);
v.setSelected(false);
} else {
textView.setTextColor(Color.BLUE);
v.setSelected(true);
}
}
});
And here's my layout:
<LinearLayout 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">
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some text"
android:textSize="20sp"
android:textColor="#FF0000"/>
</LinearLayout>
EDIT:
For your specific case would be like:
View previousView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView previousText = (TextView) previousView;
TextView curText = (TextView) v;
// If the clicked view is selected, deselect it
if (curText.isSelected()) {
curText.setSelected(false);
curText.setTextColor(Color.RED);
} else { // If this isn't selected, deselect the previous one (if any)
if (previousText != null && previousText.isSelected()) {
previousText.setSelected(false);
previousText.setTextColor(Color.RED);
}
curText.setSelected(true);
curText.setTextColor(Color.BLUE);
previousView = v;
}
}
};
findViewById(R.id.txt).setOnClickListener(clickListener);
findViewById(R.id.txt2).setOnClickListener(clickListener);
findViewById(R.id.txt3).setOnClickListener(clickListener);
}

Related

Why SetText not work in OnClick() function

I just know how to use Android Studio Code Yesterday. And I got a problem when I need to Change the text when clicking a button.
But when I text, Its don't work.
Here is my code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View bubbleView = getLayoutInflater().inflate(R.layout.bubble_view, null);
Button bubble = (Button) findViewById(R.id.start_bubble);
bubble.setOnClickListener(this);// calling onClick() method
Button predict = (Button) bubbleView.findViewById(R.id.predict);
predict.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.start_bubble:
startService(new Intent(getApplicationContext(), SimpleService.class));
case R.id.predict:
View bubbleView = getLayoutInflater().inflate(R.layout.bubble_view, null);
TextView predict_text = (TextView) bubbleView.findViewById(R.id.predict_text);
predict_text.setText("Hi"); // <--- It don't work :(
default:
break;
}
}
}
[EDIT] []
Add some .XML file
Here is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.siddharthks.sampleapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/start_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
</LinearLayout>
and here is my bubble_view.xml, its just for a
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="16dp"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/predict"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
<TextView
android:id="#+id/predict_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="16dp"
android:textColor="#AA000000"
android:textSize="21sp" />
</LinearLayout>
</ScrollView>
Do you have any suggested for me ?
I'm not sure why you inflate the "bubble_view.xml" layout in the activity class. But as your question, there are two main methods to make the button clickable. There is a good explanation in your first comment which is done by Mike M. Once you inflate a layout, it will create a new instance.
Fist answer, Assuming you want everything inside the activity.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button bubble;
private Button predict;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUIViews() // Initialze UI Views
initUIActions() // Initialize Ui Actions
}
private void initUiViews() {
Button bubble = (Button) findViewById(R.id.start_bubble);
Button predict = (Button) bubbleView.findViewById(R.id.predict);
}
private void initUIActions() {
bubble.setOnClickListener(this);// calling onClick() method
predict.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.start_bubble:
startService(new Intent(getApplicationContext(), SimpleService.class));
break;
case R.id.predict:
predict_text.setText("Hi");
break;
default:
break;
}
}
}
and restructure your XML layout as follow. There are few ways to restructure these layouts, I'll write the easiest way, but note that this is not the optimal way.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.siddharthks.sampleapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/start_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
<!-- include bubble layout file -->
<include layout="#layout/bubble_view.xml" />
</LinearLayout>
Other than the include tag you can add the whole code inside to the Activity layout.
The second answer, Assuming you want activity and Service with a bubble view.
If you are looking for a bubble view, You have to create a Bubble service.
Check this answer: Bubble Example
Official Doc: Android Bubble
Try This it will add Your bubble view in your parent and you can perform any action on that particular Layout from main Layout.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout parent = findViewById(R.id.activity_main); //parent layout.
View childView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.bubble_view,parent,false);
parent.addView(childView);
Button predict = (Button) childView.findViewById(R.id.predict);
TextView predict_text = (TextView) childView.findViewById(R.id.predict_text);
predict.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
predict_text.setText("Hi"); // <--- It don't work :(
}
});
}
}
Add break to the each case statement in the switch.
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
TextView predict_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button) findViewById(R.id.start_bubble);
LinearLayout parent = findViewById(R.id.activity_main);
View childView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.bubble_view, parent, false);
parent.addView(childView);
Button predict = (Button) childView.findViewById(R.id.predict);
predict_text = (TextView) childView.findViewById(R.id.predict_text);
predict.setOnClickListener(this);
start.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.predict:
predict_text.setText("Hi");
break;
}
}
}

getWidth() returns 0 even after applying a workaround

My java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
final TextView textView = (TextView) findViewById(R.id.text1);
final Button button = (Button) findViewById(R.id.button1);
button.post(new Runnable() {
#Override
public void run() {
int buttonWidth = button.getWidth();
int textWidth = textView.getWidth();
button.setWidth(buttonWidth-textWidth);
}
});
My xml views:
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Timer"
android:textSize="16sp"/>
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="+5"/>
I'm using the second way as described in this answer.
What I want to do is have the button fill the entire width with just enough space for the textView. I've been learning Android for the last few months only so it'll be helpful if you could explain in a lucid manner.
you can use a linearlayout with your button having a weight of 1 and your textview to wrap content.
try this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some text" />
</LinearLayout>
Use the addOnGlobalLayoutListener instead from the solution you linked, that usually works for me when I see this issue.
myView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT > 16) {
myView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
else {
myView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
myView.getWidth();
}
});

How to make one ImageView invisible when another clickable ImageView is pressed?

I tried to make ImageView(bul1) disappear when ImageView(Seethrough) is pressed. I get a nullpointer error when i try to run this code. What is wrong with it?
JAVA code
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView seethrough1 = (ImageView) findViewById(R.id.Seethrough);
final ImageView view1 = (ImageView) findViewById(R.id.bul1);
seethrough1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(view1.getVisibility() == View.VISIBLE)
{
view1.setVisibility(View.INVISIBLE);
}
}
});
}
XML code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:paddingBottom="6dp"
android:src="#drawable/gun"
android:clickable="true"
android:id="#+id/Seethrough"
android:onClick="next"
/>
<ImageView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:src="#drawable/bullet"
android:id="#+id/bul1"
/>
</LinearLayout>
You need to reconcile seethrough's onClickListener with its onClick XML attribute. I'd suggest removing this line from the xml:
android:onClick="next"
and placing the code inside your next method (if you have one)
public void next (View v){
some code
}
behind or before your visibility checking if, whichever suits you more:
#Override
public void onClick(View v) {
//place some code here
if(view1.getVisibility() == View.VISIBLE){
view1.setVisibility(View.INVISIBLE);
}
//or here
}
I think their is problem with out xml code, please try writing xml as follows,
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:paddingBottom="6dp"
android:src="#drawable/gun"
android:clickable="true"
android:id="#+id/Seethrough"
android:onClick="next"
/>
<ImageView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:src="#drawable/bullet"
android:id="#+id/bul1"
/>
</LinearLayout>
If it return NullPointerExeption, I think your ImageView is Null, because
setContentView(R.layout.activity_main);
and activity_main.xml is not like contents of your post, check name of layout and try again.
I found out that I was supposed to declare the imageviews inside the method instead of before.
like this
public void onClick(View v) {
ImageView seethrough1 = (ImageView) findViewById(R.id.Seethrough);

Textview Gravity not working properly in android

What's wrong with my code, I'm trying to display my TextView named "invalid", in different locations (left,right,center), but the gravity (left,right,center) won't work!
My text.xml is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/etext"
android:hint="#string/comment"
android:inputType="textPassword"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100">
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/button"
android:layout_weight="25"/>
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="ToggleButton"
android:layout_weight="75"
android:checked="true"
android:paddingLeft="15dp"/>
</LinearLayout>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/invalid"
android:layout_gravity="center"
android:gravity="center" />
</LinearLayout>
My TextPlay.java is
public class TextPlay extends Activity {
Button button;
ToggleButton tbutton;
TextView tview;
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
button = (Button) findViewById(R.id.button1);
tbutton = (ToggleButton) findViewById(R.id.toggleButton1);
tview = (TextView) findViewById(R.id.textView1);
et = (EditText) findViewById(R.id.etext);
tbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (tbutton.isChecked()) {
et.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
} else {
et.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
}
});
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String input = et.getText().toString();
System.out.println(input);
if (input.contentEquals("left")) {
tview.setGravity(Gravity.LEFT);
} else if (input.contentEquals("right")) {
System.out.println("inside right");
tview.setGravity(Gravity.RIGHT);
} else if (input.contentEquals("right")) {
tview.setGravity(Gravity.CENTER);
}
}
});
}
}
You set this text view a width of "wrap_content" it means, what ever the text is, the view take the size of the text.
and in the LinearLayout , the default gravity (used here) is 'center'
you should try this :
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent" <= change this
android:layout_height="wrap_content"
android:text="#string/invalid"
android:gravity="center" <= then this gravity will be taken into account
/>
99% of the time, not working properly == not used properly.
You are mistaking gravity and layout_gravity.
gravity is the way the text will align itself in the TextView. The TextView being in wrap_content this does nothing, as the TextView is exactly the size of the text.
layout_gravity is the way the TextView will align itself in its parent, in your case in the vertical LinearLayout
You have given your TextView width wrap_content, and that is the problem, Check below code and replace it to your code.
<TextView
android:id="#+id/txtInvalid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/invalid"
android:gravity="center"
/>
One reason why gravity does not work properly can be if you have gravity and textAlignment
If you put it like this
<TextView
android:id="#+id/textView1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center|center_horizontal"
android:textAlignment="viewStart"
Android studio editor will give this warning:
Inconsistent alignment specification between textAlignment and gravity attributes: was center|center_horizontal, expected start (Incompatible direction here)
But if you add textAlignment inside the styles then it doesn't complain.
This can be the issue why your gravity value will not work
<TextView
android:id="#+id/textView2"
style="#style/Widget.App.TextView.Label.Default"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center|center_horizontal"
Check this answer for the difference between them:
Text/Layout Alignment in Android (textAlignment, gravity)
set android:layout_width="fill_parent" for textView1
Make sure you don't have something like that hanging around
app:layout_box="left|top"

Change textview dynamically in Viewflipper

pretty easy question here, but I am not a java superuser quite yet.
I am using a ViewFlipper to provide a number of images, and a sliding-drawer to contain text specific to each image. I would like to have the text of the sliding drawer change to a specific string dependent on which child view is currently displayed.
Here is the 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="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prev" />
<Button
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next" />
</LinearLayout>
<ViewFlipper
android:id="#+id/flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="#+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="#drawable/image1"></ImageView>
<ImageView android:id="#+id/imageView2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="#drawable/image2"></ImageView>
</ViewFlipper>
</LinearLayout>
here is the java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
flipper = (ViewFlipper)findViewById(R.id.flipper);
next = (Button) findViewById(R.id.next);
previous = (Button) findViewById(R.id.previous);
imageview1 = (ImageView)findViewById(R.id.imageView1);
imageview2 = (ImageView)findViewById(R.id.imageView2);
next.setOnClickListener(this);
previous.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == next) {
flipper.setInAnimation(inFromRightAnimation());
flipper.setOutAnimation(outToLeftAnimation());
flipper.showNext();
}
if (v == previous) {
flipper.setInAnimation(inFromLeftAnimation());
flipper.setOutAnimation(outToRightAnimation());
flipper.showPrevious();
}
}
I'm sure the answer is really obvious, but that's why I need your help....THANKS!
When you call showNext() and showPrevious(), update your TextView to match.

Categories