Android Textview marquee not working for first time - android

I have Textview it is intially hidden when activity loads.. When click on button it is shown..
But first time when textview is showing marquee doesnot work.. Unless it is working fine.. If screen get locked after unlocked it started working fine..
I am setting string as text in code and have also used setselected(true) in code..
<TextView
android:id="#+id/txtInfo"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="#id/linearTtitle"
android:background="#color/md_grey_300"
android:ellipsize="marquee"
android:freezesText="true"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:text=""
android:textColor="#color/md_black_1000"
android:textSize="16sp" />`

When you setSelected true to your textview at that time textview is not in a position to perform command so you can do it inside view.post so when it is active it will perform the operation.
Try this code inside your button
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
tv.setVisibility(View.VISIBLE);
tv.post(new Runnable() {
#Override
public void run() {
tv.setSelected(true);
}
});
}
});

Related

Non-responding EditText

In my activity , an EditText is invisible by default and once a button is clicked it becomes visible.
I managed to do that but once the EditText is visible , I can't input text to it.In other words, the soft keyboard never appears ,the writing cursor never appears , and the hint inside it never goes and it's like "frozen".
I tried the following but didn't solve my problem
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editsearch.setVisibility(View.VISIBLE);
editsearch.setEnabled(true);
editsearch.setFocusable(true);
}
});
and this is my xml
<EditText
android:id="#+id/search"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#color/colorAccent"
android:drawableEnd="#drawable/ic_search"
android:drawablePadding="3dp"
android:layout_alignParentTop="true"
android:drawableRight="#drawable/ic_search"
android:visibility="invisible"/>
Try adding editsearch.requestFocus() method to your code after the line editsearch.setFocusable(true).
I found the solution.
I use relative layout and below my Edittext , there is a listview. I found that the Edittext was behind the listview so I brought it to front.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editsearch.setVisibility(View.VISIBLE);
editsearch.requestFocus();
editsearch.bringToFront();
editsearch.invalidate();
}
});

Android Marquee Wait duration

How to make a Marquee TextView wait for a specific time until it starts to scroll horizontally?
Because when I open an Activity it starts straight to scroll. So you have to wait until its back on startposition to read it.
In the XML I simply added TextView like this:
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!, Hello World!, Hello World!, Hello World!, Hello World!, Hello World!, Hello World!, Hello World!"
android:ellipsize="marquee"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:freezesText="true"
android:maxLines="1"
android:scrollbars="none" />
Then in code (in Activity, but can be anywhere):
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
textView.setSingleLine(false);
textView.setMaxLines(1);
}
#Override
protected void onResume() {
super.onResume();
textView.postDelayed(new Runnable() {
#Override
public void run() {
textView.setSingleLine(true);
}
}, 3000);
}
As mentioned in this answer in order to activate textview marquee you have to add this :
tv.setSelected(true);
As you want to start the marquee with a delay you have to put this inside your run() like this
tv.postDelayed(new Runnable() {
#Override
public void run() {
tv.setSelected(true);
}
}, 1000);
this will delay it
<TextView
android:id="#+id/testing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="Some veryyyyy long text with all the characters that cannot fit in screen, it so sad :( that I will not scroll"
android:layout_below="#id/toolbar"
/>
Then, in activity just set using setSelection to true. I already tested it and its working
testing.postDelayed(new Runnable() {
#Override
public void run() {
testing.setMaxLines(1);
testing.setEllipsize(TextUtils.TruncateAt.MARQUEE);
testing.setMarqueeRepeatLimit(10000);
testing.setSelected(true);
}
}, 3000);
All you need to do is delay the focus of textview so that it starts it marquee after a certain period of time. The following code starts rolling the marquee after 2 seconds.
yourTextview.postDelayed(new Runnable() {
#Override
public void run() {
yourTextview.setSelected(true);
}
}, 2000);
P.S : You first need to post what you have tried so far.

Run TextView marquee after some time

How can I run a text marquee in the TextView with delay before start?
At this moment I use the next code to start:
mTVTitle.postDelayed(new Runnable() {
#Override
public void run() {
mTVTitle.setFocusableInTouchMode(true);
mTVTitle.invalidate();
}
}, 1000);
TextView xml:
<TextView
android:id="#+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:marqueeRepeatLimit="2"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAppearance="?attr/titleTextAppearance"
android:textColor="#color/white"/>
But it doesn't work although if I set this property in xml then all right. How to fix it to I can start a marquee programmatically?
As mentioned here in order to activate textview marquee you have to add this :
mTVTitle.setSelected(true);
As you want to start the marquee with a delay you have to put this inside your run() like this
mTVTitle.postDelayed(new Runnable() {
#Override
public void run() {
mTVTitle.setSelected(true);
}
}, 1000);

Android - Change Image View On Click

I have a set of image view that I want to change when it is clicked. The first view is set by default, and I want the second one to appear when the first one is clicked. I also want the reverse to occur when the second one is visible. How can I do this? (Example: Default is Img 1 default, img 2 hidden. On click, img 2 shows, img 1 is hidden. On click again, reverse occurs).
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_alarm_off_75dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="134dp" />
<ImageView
android:id="#+id/img2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_alarm_on_75dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:visibility="gone"
android:layout_marginTop="134dp" />
Use the Following Code:
ImageView img1=(ImageView)findViewById(R.id.img1);
ImageView img2=(ImageView)findViewById(R.id.img2);
img1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
img1.setVisibility(View.GONE);
img2.setVisibility(View.VISIBLE);
}
});
img2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
img2.setVisibility(View.GONE);
img1.setVisibility(View.VISIBLE);
}
});
in both of button onclick you must check to see if other button is not visible then set it visible and if it is visible then do not do any thing. you can check visibility of buttons with this code
if(mybutton.getVisibility()==View.VISIBLE)
{
//set visibiliy of mybutton
}

How to disable other button until a button is clicked

I am new in android programming. I have made an app which is fill in the blanks type app. Until the answer-confirm button is clicked, the next and the previous Button should be disabled. If it is clicked and answer is checked, then next and previous Button to get enabled. please help!!!!!!!!
If you want to disable a button in xml use this code
<Button
android:text="Next"
android:id="#+id/my_button_del"
android:layout_width="72dp"
android:layout_height="40dp"
android:visibility="invisible"/>
for enabling the button when we click on previous then in onClick function(previous) add this code
next.setVisibility(View.VISIBLE);
next is the button
something like that (maybe its not the best way, you can play with it and make it better)
protected void onCreate(Bundle savedInstanceState) {
...
firstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
enableNextButton();
}
});
...
}
private void enableNextButton(){
nextButton.setBackgroundResource(R.drawable.button_active);
nextButton.setClickable(true);
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goNext();
}
});
}
private void disableNextButton(){
nextButton.setBackgroundResource(R.drawable.button_inactive);
nextButton.setClickable(false);
}
and in your xml the buttons should be something like
<Button
android:id="#+id/first_button"
android:layout_width="match_parent"
android:layout_height="#dimen/generic_button_heigth"
android:background="#drawable/button_active"
android:layout_margin="#dimen/generic_margin"
android:text="#string/first_button_text"
android:textColor="#color/white"/>
<Button
android:id="#+id/next_button"
android:layout_width="match_parent"
android:layout_height="#dimen/generic_button_heigth"
android:background="#drawable/button_inactive"
android:clickable="false"
android:layout_margin="#dimen/generic_margin"
android:text="#string/next_button_text"
android:textColor="#color/white"/>
this way you start with an active button and an inactive button, when the first is pressed you can "activate" the next button
Please use punctuations and uppercases but anyway.
You can check here for more infos about buttons : http://developer.android.com/reference/android/widget/Button.html
Otherwise, the method setVisible() allow you to make visibile or not a button of your layout. 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"/>
EDIT
Oh sorry ! So you can use setEnabled().

Categories

Resources