How can I align the text in a TextSwitcher? - android

I want to justify my TextSwitcher in android. But I am not getting any solution for that. I have added a TextSwitcher like below-
<TextSwitcher
android:id="#+id/ts_place"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="#dimen/left_offset"/>
But the text showing with no alignment both left and right like below-
The Starks ruled the North until the betrayal of Robb Stark at the Red
Wedding by Roose Bolton. Since then, it has been the Boltons who rule
the North with their seat shifted to Winterfell from the Dreadfort.
Karstark, Manderly, Umber, Reed and Mormont are the major vassal
houses who swear allegiance to the Warden of the North.
I want make these text left and right aligned properly.

Create a ViewFactory and override the makeView function to use your own custom view/textview for your text switcher. You should be able to set properties on that view.
TextSwitcher exampleTextSwitcher;
exampleTextSwitcher.setFactory(textSwitcherFactory);
private ViewSwitcher.ViewFactory textSwitcherFactory = new ViewSwitcher.ViewFactory()
{
#Override
public View makeView()
{
LayoutInflater inflater = LayoutInflater.from(context);
return inflater.inflate(R.layout.view_timer_textview, null);
}
};
view_timer_textview.xml
<TextView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/test"
android:includeFontPadding="true"
android:textAlignment="center"
android:layout_gravity="center"
android:lines="1"
android:ellipsize="end"/>

You can also do it directly through code for example:
TextSwitcher textSwitcher = findViewById(R.id.your_id_text_switcher);
textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
#Override
public View makeView() {
tvSwitcher = new TextView(MainActivity.this);
tvSwitcher.setTextColor(getResources().getColor(R.color.textColor));
tvSwitcher.setTextSize(18);
//edit lines as follows
tvSwitcher.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
return tvSwitcher;
}
});

Related

Customized Android Widget

I need to create a Widget which look like photo below:
Upper part of photo resembles normal state of my costume widget and lower part of it resembles highlight state of the widget.
Widget primary features:
Both Normal and Highlighted sates could have a background with image and color at same time.
It could re-size by width easily (for example 200dp or 100dp)
I have 3 questions:
Is there any particular widget which could help me to create something like what I said?
Or should I create a custom widget myself?
Create a custom layout with a Vertical LinearLayout and two child TextView for Title and description.
Set the description TextView visibility as GONE , since you need to show the description only on highlight state.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_highlight"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#drawable/image_shape">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textStyle="bold"
android:textSize="30dp"
android:layout_margin="10dp"/>
<TextView
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello! Iam here to display the description"
android:textStyle="italic"
android:textSize="25dp"
android:layout_margin="5dp"
android:visibility="gone"/>
</LinearLayout>
Now, on the code change the description TextView visibility as visible on long click.
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.layout_highlight);
final TextView descriptionTextView = (TextView)findViewById(R.id.description);
linearLayout.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
descriptionTextView.setVisibility(View.VISIBLE);
return true;
}
});
Also , you need to hide back the description when long click is released. so you have to have onTouch listener like this..
linearLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
descriptionTextView.setVisibility(View.GONE);
}
return false;
}
});

How to disable blinking of textview onClick

I have a TextView with an onClickListener().
When I click on the TextView it blinks.
How to disable this effect?
Here is the XML
<TextView
android:id="#+id/descriptionText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/background"
android:ellipsize="end"
android:paddingTop="4dp"
android:paddingLeft="8dp"
android:maxLines="3"
/>
I tried to remove android:ellipsize and android:maxLines tags - no effect.
And here is the code:
accountDescriptionTextView = (TextView) v.findViewById(R.id.descriptionText);
accountDescriptionTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (descriptionOpened)
{
// accountDescriptionTextView.setEllipsize(TextUtils.TruncateAt.END);
// accountDescriptionTextView.setMaxLines(3);
descriptionOpened = false;
}
else
{
// accountDescriptionTextView.setEllipsize(null);
// accountDescriptionTextView.setMaxLines(100);
descriptionOpened = true;
}
}
});
I need to have the commented functionality, but even when this lines are commented I still see how the textview blinks.
The text just disapears when i place my finger on the screen and apears when I take the finger away.
Android uses a selector to give different colors to widgets' pressed state.
If you don't want that behavior, you can use solid colors for android:textColor and android:textColorHighlight.
Check the TextView doc.

View moving from one end of screen to other

I need to create a view that is moving from one end of screen to other end at the bottom of screen.
Means like in new channels flash news is moving contiguously at bottom.Similar concept is what i want.
I dont know what widget is to be used.I tried flipper but in that only 1 textview is replacing by other only.I need it to move from one end to other and change the content.
Can anyone help?
I tried with the below mentioned answer using marquee..but still it is not moving..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t = (TextView) findViewById(R.id.label);
t.setSelected(true);
}
//xml
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:text="#string/hello_world"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
/>
it worked.
Problem was actually i gave a string which is very small (hello world) now i give a new lengthy string.so it worked
You can do this.
xml for TextView
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true" />
code for this TextView
If you want to make a ticker
textView.setSelected(true);
If you want to stop it
textView.setSelected(false);
*** * EDIT ** ****
I assume that you are extending your activity from ListActivity. Do the following In your onListItemClick
public void onListItemClick(ListView parent, View row, int position, long id) {
TextView textViewOne = (TextView)row.findViewById(R.id.text_view);
textViewOne.setSelected(true);
for (int i = 0; i < parent.getChildCount(); i++) {
if(i!=position){
View view = (View) parent.getChildAt(i).findViewById(R.id.view);
textViewOne = (TextView)view.findViewById(R.id.text_view);
textViewOne.setSelected(false);
}
}
}
just check it out :
xml file :
<TextView
android:id="#+id/myTextView"
android:ellipsize="marquee"
android:singleLine="true"/>
in code :
tv = (TextView) findViewById(R.id.myTextView);
tv.setSelected(true);
here you have to take textview as single line
I don't totally understand what you want to achieve but apparently you have two options:
create your own view: http://developer.android.com/training/custom-views/index.html
or create some animation and use an existing view: http://developer.android.com/training/animation/index.html

Show a Button seeMore When the text in the text view goes out of view

I want to show a Button seeMore when the text in the text view is larger than one line or when the text in the text view goes out of the view.I have done it using character length and substring method. But the problem is when it comes with different screen size, i was not able to to determine the string length. Now i am using a fixed length for four different screen sizes mainly medium,Normal ,large Xlarge. Could anyone help me to overcome this issue
Thanks inadvance....
I have implememented a similar functionality recently, where I needed to show a list of users' comments. Each item would show a max of two lines and "more" link. When that link was clicked, the full text would be shown and "more" link hidden.
First, I had an array of Comment objects:
public class Comment {
private boolean showFull;
private String name;
private String date,
private String description;
//standard constructor and a set of setters and getters, including
public String getFullDescription();
public String getShortDescription();
}
Now, in this particular implementation, the short description was just the first 100 chars of the long description with '...' appended (if the overall length was more than 100 chars).
I used the array of these Comment objects as the datasource for a custom Adaper:
public class CommentRowAdapter extends BaseAdapter {
private List<Comment> data = null;
...
//all standard method implementations, including get, count, etc., etc. and then
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout row = (LinearLayout) (convertView == null ? LayoutInflater
.from(context).inflate(R.layout.listcomm, parent, false)
: convertView);
row.setClickable(false);
final Comment comment = data.get(position);
//populate all other elements of the row
...
//and now the description
if (comment.isShowFull()) {
TextView tv = (TextView) row.findViewById(R.id.CommentDesc);
tv.setText(comment.getDescriptionFull());
tv.setTextColor(context.getResources().getColor(R.color.black));
tv = (TextView) row.findViewById(R.id.CommentMore);
tv.setVisibility(View.GONE);
} else {
final TextView tvDesc = (TextView) row.findViewById(R.id.CommentDesc);
tvDesc.setText(comment.getDescriptionShort());
tvDesc.setTextColor(context.getResources().getColor(R.color.black));
final TextView tvMore = (TextView) row.findViewById(R.id.CommentMore);
tvMore.setVisibility(View.VISIBLE);
tvMore.setTextColor(context.getResources().getColor(R.color.venue_blue));
tvMore.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
comment.setShowFull(false);
tvDesc.setText(comment.getDescriptionFull());
tvMore.setVisibility(View.GONE);
tvDesc.invalidate();
}
});
}
return row;
}
}
The XML for the row was
<LinearLayout android:id="#+id/ListPoi"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:padding="5dp"
android:background="#drawable/listpoi_color" xmlns:android="http://schemas.android.com/apk/res/android">
/>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="#+id/CommentName" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="12sp"
android:gravity="left" android:textStyle="bold" android:text="Name"
android:singleLine="true" />
<TextView android:id="#+id/CommentDate" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="12sp"
android:paddingLeft="5dp" android:textStyle="bold" android:text="Date" android:singleLine="true" />
</LinearLayout>
<TextView android:id="#+id/CommentDesc" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="bottom"
android:textSize="12sp" android:text="Description" />
<TextView android:id="#+id/CommentMore" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="right"
android:textSize="12sp" android:text="more" />
</LinearLayout>
The layout for different screen sizes was taken care of by the list itself.
You can extend this implementation by not limiting the size of text by number of characters but rather by the height of the text field. This question has a very good answer about how to compute the size of text in a text view. Using that technique, you can determine the number of characters that you need to use for truncation. Other than that, it's the ListView itself that's responsible for the layout in different screen sizes.

how to add another view using layout inflatter

I created mainLayout with two buttons
add: to add other layout
remove : to remove other layout.
<Button
android:id="#+id/btnAdd"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Add View"
android:onClick="addView" />
<Button
android:id="#+id/btnRemove"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Remove View"
android:onClick="removeView" />
now i wrote following code to add the view
when I click on addView button
LayoutInflater inflater= (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.other_layout,null);
mainLayout.addView(view);
the view is added below the main layout.
But I want the view to add in middle of the screen not at the bottom of screen.
How can I do that?
Modify this line and put ur parent view.
view=inflater.inflate(R.layout.other_layout,PARENT_VIEW_OBJECT);
It should work
get the parentLayout like this
parentLayout=(YourParentLayout)view.findViewById(R.id.parentLayout);
then
parentLayout.addView(yourview);
You can check below code, its working fine for me
private View view = null ;
Button addbutton = null;
ViewGroup parent = null;
LayoutInflater inflater = null;
inflater= (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
addbutton = (Button)findViewById(R.id.btnAdd);
parent = (ViewGroup) findViewById(R.id.mainxml);
addbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
view = inflater.inflate(R.layout.other, null);
parent.addView(view);
}
});
I would put an empty LinearLayout placeholder at the top of your main layout where you want your view to be and add the view to that instead of the main layout.
You can adjust the placeholder first and modify it's location and looks.
<LinearLayout
android:id="#+id/placeholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
..../>
<Button
..../>
I would look into using a ViewStub it holds a place until .inflate() is called. That way the layout isn't taking resources until it's needed.

Categories

Resources