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
Related
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;
}
});
Please bear with me as I am new to the use of Views and Layouts.
I am trying to create an application in which the user can enter a text field, press a button and have a new text field appear and they can continue adding text fields in this way.
My solution was to have the top level be a scrollview and then have a relative view as a child within that(this way I can then programatically insert more editviews in my code with the OnClick() listener.
I have seen and read a couple of other posts pertaining to relative views but it seems there is still something that I am missing. I have tried
Here is the xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_create_accounts"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.nic.mybudget.CreateAccountsActivity">
<RelativeLayout
android:id="#+id/activity_create_accounts_relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/activity_name"
android:inputType="textAutoComplete"
android:layout_alignParentTop="true"
android:id="#+id/activity_createAccounts_relativeLayout_activityName"/>
<Button
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="#+id/activity_createAccounts_relativeLayout_activityName"
android:id="#+id/activity_create_accounts_relativeLayout_activityName_addButton" />
<Button
android:text="Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/activity_create_accounts_relativeLayout_activityName_addButton"
android:layout_centerHorizontal="true"
android:id="#+id/activity_create_accounts_relativeLayout_activityName_saveButton" />
</RelativeLayout>
And here is the Code where I try to add new editviews.
public class CreateAccountsActivity extends Activity {
static private final String TAG = "MAIN-Activity";
int numAccounts = 0;
int lastAccountID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_accounts);
final RelativeLayout Relative = (RelativeLayout) findViewById(R.id.activity_create_accounts_relativeLayout);
final TextView oldAccount = (TextView) findViewById(R.id.activity_createAccounts_relativeLayout_activityName);
final TextView newAccount = new TextView(this);
final Button addNewAccountButton = (Button) findViewById(R.id.activity_create_accounts_relativeLayout_activityName_addButton);
addNewAccountButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "addNewAccountOnClick");
numAccounts = numAccounts+1;
int newAccountID = oldAccount.getId() + numAccounts;
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
newAccount.setLayoutParams(rlp);
newAccount.setHint("Hint" );
newAccount.setId(newAccountID);
Relative.addView(newAccount);
RelativeLayout.LayoutParams blp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
blp.addRule(RelativeLayout.BELOW, newAccountID-1);
addNewAccountButton.setLayoutParams(blp);
}
});
}
}
As you can see what I am trying (and failing) to do is add the new edit view at the top of the page and simply push everything else down the page. What am I getting wrong here with the relative layout?
Any help is appreciated.
First thing View with id activity_createAccounts_relativeLayout_activityName is EditText and you are casting it with TextView so that is wrong cast it to EditText.
And to your actual problem:
You are using same EditText instance with variable newAccount and adding it again in relative layout if you want to add one more EditText in relative layout you have to initialise EditText inside onclicklistener.
Just add one line newAccount= new EditText(context)in your onclicklistener code before line numAccounts = numAccounts+1;
Happy Coding !
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.
I'm trying to retrieve the getText() value of a TextView defined in my ListItem Layout, inside the setOnItemClickListener method.
Basically, I want to log data (Id, LookupKey, DisplayName, or PhoneNumber) allowing me to retrieve the contact selected regardless of the listview/adapter used.
I display a simple TextView and an Imageview which visibility is set according to the data.
here is the layout : contact_entry.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:baselineAligned="false"
android:layout_width="fill_parent" android:padding="5px">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="16px"
android:text="#+id/contactEntryText" android:id="#+id/contactEntryText">
</TextView>
<TableRow android:paddingLeft="5px" android:visibility="visible"
android:paddingRight="5px" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/indicator_logo_table"
android:gravity="right">
<ImageView android:layout_width="wrap_content"
android:id="#+id/imageView1"
android:layout_height="fill_parent"
android:src="#drawable/indicator">
</ImageView>
</TableRow>
</LinearLayout>
the adapter i use is defined below :
SimpleAdapterWithImageView adapter = new SimpleAdapterWithImageView(
this, R.layout.contact_entry, cursor, fields,
new int[] { R.id.contactEntryText });
mContactList.setAdapter(adapter);
( You could have guessed it's for displaying the contact's Display_Name from the ContactsContract ContentProvider. )
and here is finally the mContactList.setOnItemClickListener ...
mContactList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
LinearLayout linear = (LinearLayout) view.findViewById(R.layout.contact_entry);
Log.i("done","linear layout"); // it's not done at all ! findViewById returns null;
//this part is currently useless
TextView displayed_name = (TextView) linear.findViewById(R.id.contactEntryText);
Log.i("done","Displayed name = linear.findViewById contactentrytext");
Toast.makeText(getApplicationContext(),
// displayed_name.getText(), //
//view.getContentDescription(), //
Toast.LENGTH_SHORT).show();
}
As you can see i'm only trying to display a toast for the moment, in the end the name will be put as an extra for an intent.
So : the best result i got these last 36 hours (sigh) is displaying the name of the first contact displayed in the toast, even when clicking on the second item...
TextView displayed_name = (TextView) findViewById(R.id.contactEntryText);
Toast.makeText(getApplicationContext(),displayed_name.getText(),
Toast.LENGTH_SHORT).show();
}
I've been trying stuff like the tutorial google provided here, which (am I wrong? I'm new to Android so don't hesitate to slap me in the face ...) actually casts the whole View in a TextView ...
plus other tricks like view.getChildAt() and so on... for hours.
I'm convinced it's not that hard, yet i'm running in circles !
I don't think it is because of my adapter, but if anyone thinks it could be the cause, i could provide the code in another post.
Thanks in advance !
LinearLayout linear = (LinearLayout) view.findViewById(R.layout.contact_entry);
Here is a mistake - you shan't use R.layout in findViewById. Usually, you need to use R.id there, or replace findViewById by inflating a layout. But in this case you just need the view from arguments.
Replace the quted code with this one:
LinearLayout linear = (LinearLayout) view;
hi i have an activity and in the activity i have some buttons and textviews, and i would like to draw a rectangle with text inside. i saw some examples online but the all say to create my on view , override onDraw and the set this view as my layout, but i have my layout already.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView baramzona = (TextView) findViewById(R.id.TextView01);
baramzona.setText(R.string.baram_zona_textview);
final Button pocniparking = (Button) findViewById(R.id.ButtonStart);
final TextView momentalnazona = (TextView) findViewById(R.id.TextView02);
//momentalnazona.setText("Моментално се наоѓате во зоната");
pocniparking.setText(R.string.btn_Start_Parking);
pocniparking.setEnabled(false);
}
}
any ideas?
This is a kind of a workaround but it suits my purpose. Basically you can put the TextView inside a table, set the table background for the outline and the margins for the size of the outline. Hope it helps.
<TableLayout android:id="#+id/TableLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#55771B">
<TextView android:text="#+id/TextView01"
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3px"
android:layout_marginBottom="3px"
android:layout_marginRight="3px"
android:layout_marginTop="3px"
android:background="#010101"/>
</TableLayout>