Adding TextViews at the end of an AsyncTask - android

I need to add text to a ScrollableLayout after it's remotely retrieved from an AsyncTask. Since I don't know the number of strings involved, I need to programmatically create as many TextViews as needed.
Layout code
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/view_phone_mainView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/view_phone_linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top" >
<TextView
android:id="#+id/view_phone_lblTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:text="#string/view_phone_title"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/view_phone_lblWarning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/view_phone_warning"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
</LinearLayout>
</ScrollView>
Activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pDialog = new ProgressDialog(this);
pDialog.setIndeterminate(true);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setMessage(getString(R.string.view_phone_wait));
pDialog.show();
task = new MessageLoaderTask(); //Returns String[] on callback
task.execute((Void) null);
}
public void onRulesLoaded(String[] messages) { //Directly called by OnPostExecute
LinearLayout container = (LinearLayout) findViewById(R.id.view_phone_linearLayout);
if (messages != null) {
for (String m : messages) {
TextView tv = new TextView(this);
tv.setText(m);
tv.setTextAppearance(this, android.R.attr.textAppearanceSmall);
tv.setVisibility(View.VISIBLE);
tv.setBackgroundColor(Color.TRANSPARENT);
tv.setTextColor(Color.BLACK);
container.addView(tv, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
}
}
pDialog.dismiss();
}
I am sure, by debug, that strings are correctly valued. The result is that the title label gets displayed, the warning is hidden but below there is only white......
I tried to scroll and noticed that the scrolling area is veeeeeeeeeery long, compatible with the long Lorem ipsum stub text I used for testing. If I truncate via debug one of the strings to empty (there are only 2) the scrollable area is shorter in height. I use the light theme, so I expect black text on white background.
tv.setTextAppearance(this, android.R.attr.textAppearanceSmall);
tv.setVisibility(View.VISIBLE);
tv.setBackgroundColor(Color.TRANSPARENT);
tv.setTextColor(Color.BLACK);
were added at a second time when everything was failing. No difference whether they are in place or not. What can I do to fix?
Thanks

Not sure why your ScrollView would be increasing in length if this were the sole problem, but it seems like you need to set your LinearLayout's orientation to vertical.

Use the below code.
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
add all the Strings to this EditText by keeping '\n' between the strings. So that every thing will be aligned properly.

Related

My custom Android toast shows no text

wise people!
This is my first question here. I'm stuck with a problem that seemed pretty simple to solve to me. I am not able to show a custom Toast message in my Android app.
There are two custom layouts I've created
my_toast.xml - layout file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toast_layout_root"
android:layout_width="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_height="wrap_content">
<TextView
android:id = "#+id/text"
android:background="#color/yumist_red"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:textColor="#fff"
android:text = "ToastText"
android:gravity = "center"
/>
</LinearLayout>
my_toast_up.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toast_layout_root"
android:layout_width="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_height="wrap_content">
<ImageView
android:layout_width="30dp"
android:layout_height="10dp"
android:src="#drawable/chat_arrow"
android:rotation="180"
/>
<TextView
android:id = "#+id/text"
android:background="#color/yumist_red"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:textColor="#fff"
android:text = "ToastText"
android:gravity = "center"
/>
</LinearLayout>
The only difference in the two is an imageview containing an arrow image. I'm trying to create text-bubble-style Toast.
I am well able to show and use the first one in my app. But, when I use the second layout with the image, all I see is the Image and an empty TextView of the ImageView's width and standard height. I've tried a lot of posts and existing questions online, but I cannot find a solution to this.
Any help?
[java code for showing Toasts]
public static void showToast(String message, int type)
{ //'message' is the text to display, 'type' determines which of the three toasts is shown
/*
* There are three fixed toasts:
* 1. One pointing upwards (with an angle) and placed near the top. (upperNotification)
* 2. Placed in the default position, to show general messages. (no issues with this | mToast)
* 3. Placed to point at bar at the bottom. (lowerNotification)
*/
TextView textView = null ; //this will refer to the message TextView
//corresponding to the toast selected
Toast toast = null; //to refer to the toast to display
switch(type)
{
case MyToast.ARROW_DOWN:textView = lowerToastText; toast = lowerNotification; break;
case MyToast.ARROW_UP:textView = upperToastText; toast= upperNotification; break;
case MyToast.ARROW_NONE:textView = toastText; toast = mToast; break;
}
if(textView != null && toast != null)
{
System.out.println(message);
textView.setText(message);
toast.show();
}
else
{
System.out.println("NULL!");
}
}
//Below is the code I used to init these.
public static void setUpToast(Activity activity, LayoutInflater inflater)
{
mToast = new Toast(activity);
upperNotification = new Toast(activity);
lowerNotification = new Toast(activity);
View layout = inflater.inflate(R.layout.my_toast, null);
toastText = (TextView) layout.findViewById(R.id.text);
mToast.setView(layout);
mToast.setDuration(Toast.LENGTH_LONG);
layout = inflater.inflate(R.layout.my_toast_up, null);
upperToastText = (TextView) layout.findViewById(R.id.text);
upperNotification.setView(layout);
upperNotification.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP, 0, Dish.dpToPx(96, activity));
upperNotification.setDuration(Toast.LENGTH_LONG);
layout = inflater.inflate(R.layout.my_toast_down, null);
lowerToastText = (TextView) layout.findViewById(R.id.text);
lowerNotification.setView(layout);
lowerNotification.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, Dish.dpToPx(48, activity));
lowerNotification.setDuration(Toast.LENGTH_LONG);
}
I figured out a fix by trial-and-error myself.
In my layout, I had kept my TextViews' width to match its parent. That was causing it to shrink down the ImageView's width for some reason.
I changed it to WRAP_CONTENT and of the parent view to MATCH_PARENT and it fixed my problem.
I am, however, still curious if there's a way to show the text with the parent's width.

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

Android toggle button display wrong value after orientation change

Here is my problem.
I've got a simple activity which set a layout, and add rows in a table-layout(itself in a scroll view).
Those table-rows have a custom layout with a text-field and a toggle button.
Each toggle button has a value taken from a database, and when I first create the activity, the values are OK. But when I turn the device and then change the orientation, all the toggles-button take "false" value. I printed the values that I set in the Logcat, and the values are the good ones (those in the database).
I thought something like the layout I want is hidden behind another layout, but I made some tests and the text-fields change with new values, so I really don't understand why the toggle buttons don't work.
Here is the code :
TableRow layout :
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<RelativeLayout
android:id="#+id/relativelayout_row_parametres"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_weight="1.0">
<TextView
android:id="#+id/textview_row_parametres"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:textStyle="bold"
android:textSize="20sp"
android:layout_marginLeft="2dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
/>
<ToggleButton
android:id="#+id/togglebutton_row_parametres"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="2dp"
android:textOn="#string/togglebutton_on"
android:textOff="#string/togglebutton_off" />
</RelativeLayout>
</TableRow>
Activity Layout :
<?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">
<ImageView style="#style/header" />
<LinearLayout
android:layout_width="fill_parent" android:layout_height="0dp"
android:layout_weight="0.8"
android:background="#drawable/gradient_bg"
android:padding="10dip"
android:orientation="vertical">
<ScrollView
android:layout_width="fill_parent" android:layout_height="0dp"
android:layout_weight="0.85"
android:fillViewport="true"
android:padding="10dip"
android:layout_gravity="center">
<TableLayout
android:id="#+id/tablelayout_parametres"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/cornered_bg"
android:paddingTop="5dip"
android:paddingBottom="5dip">
</TableLayout>
</ScrollView>
<Button
android:id="#+id/button_parametres_accept"
android:gravity="center"
android:text="#string/accept_changes"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.15"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
And the activity code:
public class Parameters extends Activity {
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Map<String, Boolean> changes = new HashMap<String, Boolean>();
final Context ctx = getApplicationContext();
LanguageManager.updateConfig(this);
setContentView(R.layout.parametres);
CountryDB[] countries = Database.instance(getApplicationContext()).getCountries();
TableLayout tabLayout = (TableLayout) findViewById(R.id.tablelayout_parametres);
for(int i =0; i<countries.length; i++){
TableRow newRow = (TableRow) getLayoutInflater().inflate(R.layout.row_parametres, null);
TextView textView = (TextView) newRow.findViewById(R.id.textview_row_parametres);
ToggleButton toggleButton = (ToggleButton) newRow.findViewById(R.id.togglebutton_row_parametres);
toggleButton.setChecked(countries[i].isToSynchronize());
toggleButton.setTag(countries[i]);
Log.e("setChecked",""+toggleButton.getId()+"/"+countries[i].isToSynchronize());
textView.setText(countries[i].getLabel());
toggleButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
CountryDB countryTemp = (CountryDB) v.getTag();
changes.put(countryTemp.getLabel(), ((ToggleButton)v).isChecked());
}
});
tabLayout.addView(newRow);
TableRow rowDivider = (TableRow) getLayoutInflater().inflate(R.layout.row_divider, null);
tabLayout.addView(rowDivider);
}
Button buttonValidation = (Button) findViewById(R.id.button_parameters_accept);
buttonValidation.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Iterator<String> iterator = changes.keySet().iterator();
while(iterator.hasNext()){
String stringTemp = iterator.next();
Database.instance(ctx).updateCountry(stringTemp, changes.get(stringTemp));
}
Intent intent = new Intent(ctx, Splash.class);
String result = "restart";
String from = "parameters";
Intent returnIntent = new Intent();
returnIntent.putExtra("result", result);
returnIntent.putExtra("from", from);
setResult(RESULT_OK, returnIntent);
startActivity(intent);
}
});
}
}
In the Log.e, I print the values, and they are good, the display on togglebuttons is wrong, they are just all "false".
Thanks for your help.
Between onCreate() and onResume() , Android tries to restore the old state of the toggle Buttons. Since they don't have unique ID's , Android wont succeed and everything is false again. Try to move your setChecked() calls into onResume() ( maybe onStart() works too).
Here is a pretty good answer to the same Question:
ToggleButton change state on orientation changed
you have to save data before Orientation.
Android have method onSaveImstamceState(Bundle outState) and onRestoreInstanceState(BundleInstaceState)
override these method in Activity.
There's a simpler solution: you only need to add configChanges property to your activity declaration, like stated here. This way you can prevent Activity restart when orientation changes. So in your manifest you should have something like
<activity android:name=".Parameters"
android:configChanges="orientation|keyboardHidden">
or if your buildTarget>=13
<activity android:name=".Parameters"
android:configChanges="orientation|keyboardHidden|screenSize">
Edit: Like reported on comments below, this is not an optimal solution. The main drawback is reported in a note of the document linked above:
Note: Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications.

No scrolling effect with ScrollView in PopupWindow

I want to implement a popup menu with complex effects, one of them is to support scrolling. It seems PopupMenu and AlertDialog can not meet the requirement what I need. So I tried PopupWindow with ScrollView.
Firstly, I prepared a layout which has a simple strcture just like what ApiDemo shows:
ScrollView
LinearLayout with Vertical attribute
TextView
TextView
TextView
...
Secondarily, I new a PopupWindow with this layout and 200width/300height, show it at somewhere with showAtLocation().
I can make scrollbar displayed and has scroll effect, but TextViews in LinearLayout do NOT scroll(they are in fixed position)!
Something is wrong but I have no sense on it.
(android3.0)
Thanks for any warm-heart man who can give me some tips.
-_-!!
I also faced similar issue. Some how wrapping relative layout in scrollview is not working for popupwindow.
I tried wrapping individual views under scrollview and it worked for me. PLease have a look below. Hope this helps
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#eebd9c"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_below="#+id/textView2" >
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:ems="15"
android:gravity="fill_horizontal"
android:minLines="6"
android:scrollbars="vertical"
android:singleLine="false"
android:text="Multiline text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</ScrollView></RelativeLayout>
I think your requirement can be fullfilled by Dialog try the code i have given
protected void showInputDialog() {
final Dialog splRerDialog = new Dialog(getContext());
splRerDialog.setTitle("Special request.");
ScrollView scroll = new ScrollView(getContext());
LinearLayout lin = new LinearLayout(getContext());
lin.setLayoutParams( new LayoutParams(350,200));
lin.setGravity(Gravity.CENTER);
lin.setOrientation(LinearLayout.VERTICAL);
final EditText req = new EditText(getContext());
req.setLayoutParams( new LayoutParams(300,300));
lin.addView(req);
Button btn = new Button(getContext());
btn.setText("Ok");
btn.setLayoutParams( new LayoutParams(200,LayoutParams.WRAP_CONTENT));
btn.setGravity(Gravity.CENTER);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
splRerDialog.dismiss();
}
});
lin.addView(btn);
scroll.addView(lin);
splRerDialog.addContentView(scroll, new LayoutParams(LayoutParams.WRAP_CONTENT,200));
splRerDialog.show();
}

Android:Drawing Many Text Views

I'm new to android platform , I'd like to settext using textviews , I tried to write set text into two textviews but its just draws one textview why?
I can't draw two textviews
TextView tv1;
TextView tv2;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
layout = new LinearLayout(this);
tv1 = new TextView(this);
tv2 = new TextView(this);
tv1.setText("Hello");
tv2.setText("How are you?");
}
On Android, the user interface normally should be created using XML-files, instead of Java code. You should read up on the tutorials on android.com, especially:
http://developer.android.com/guide/topics/ui/declaring-layout.html
An example:
In your res/layout/main.xml, you define the text TextView's:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="#+id/TextView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TextView 1"/>
<TextView android:id="#+id/TextView2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TextView 2"/>
</LinearLayout>
Then if you use setContentView in the activity to display this, the app will show to TextView's:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
}
If you want to programmatically set the text in the Activity, just use findViewById():
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
((TextView)this.findViewById(R.id.TextView1)).setText("Setting text of TextView1");
}
I definitely second TuomasR's suggestion to use XML layouts. However, if you're wanting to add new TextViews dynamically (i.e. you don't know how many you will need until runtime), you need to do a couple of other steps to what you are doing:
First, define your LinearLayout in main.xml (it's just easier that way than LayoutParams, IMO):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_linear_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
/>
Now, you can go to your code, and try the following:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//This inflates your XML file to the view to be displayed.
//Nothing exists on-screen at this point
setContentView(R.layout.main);
//This finds the LinearLayout in main.xml that you gave an ID to
LinearLayout layout = (LinearLayout)findViewById(R.id.my_linear_layout);
TextView t1 = new TextView(this);
TextView t2 = new TextView(this);
t1.setText("Hello.");
t2.setText("How are you?");
//Here, you have to add these TextViews to the LinearLayout
layout.addView(t1);
layout.addView(t2);
//Both TextViews should display at this point
}
Again, if you know ahead of time how many views that you need, USE XML.

Categories

Resources