My custom Android toast shows no text - android

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.

Related

Adding views programatically to a relative layout within a scrollview

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 !

How to dinamically add ImageView/textView at the right of others ImageView/textView in a same layout

So in my code I have 6 arrays (the 5 last could be empty) of objects. Each objects has a name and the array could have many of each object (every array are sorted so every item are grouped).
First, I have this xml file:
<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"
android:background="#drawable/fond4"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:background="#color/white"
android:gravity="center"
android:text="#string/commentOpti"
android:textSize="20sp" />
<Button
android:id="#+id/choisirTroupe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:onClick="soumission"
android:text="#string/lancer" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:background="#color/white"
android:gravity="center"
android:text="#string/casernes"
android:textSize="20sp" />
<!-- Caserne 1 -->
<LinearLayout
android:id="#+id/caserne1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#color/white"
android:orientation="horizontal" >
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:background="#drawable/caserne" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="#string/deuxPoints"
android:textSize="25sp" />
</LinearLayout>
//Then 5 other like this 1st linearLayou
//The ids are incremented each time like caserne2 for the 2nd etc...
</ScrollView>
It look like this: https://gyazo.com/17a1276dfff5521d540fb6dc953df424
What I want to do is, for each object in each array (one array: one linear layout), to add a textView with the number of Item and an imageView which represent the object.
Next, you will find how I sort everything, that's not really important I think, but if you want to understand everything you'll have to give a look :p
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_optimisation);
LinearLayout caserne1 = (LinearLayout) findViewById(R.id.caserne1);
//...Doing this for the 5 others...
Intent intent = getIntent();
//Instanciation of my object
OptiClashOfClans o = new OptiClashOfClans();
//In fact, the number of linearLayout i'll have to work with
o.setNbCasernes(this.nbCaserne);
if (this.nbBarbares > 0)
o.ajouterFormation(1, this.nbBarbares);
//...Adding each object in on array...
o.setAFormer(o.triTroupe());
//And finally here i'll put each object in the good array
o.orgaCaserne(o);
Now we go for my problem, this portion of code is just next to the one I put previously, here is how I pick up the number of the same object in each array
for (int cptCaserne = 0; cptCaserne < this.nbCaserne; cptCaserne++) {
// Here I pick up the array of object I'll work with
Caserne casTmp = o.getCaserneNum(cptCaserne);
// Here I pick every object which are in the previous array
ArrayList<Troupe> enFormTmp = new ArrayList<Troupe>();
enFormTmp = casTmp.getEnFormation();
// To count where I am in the array of object
int cptAFormer = 0;
//To know if the next object is different from the one I'm working with
Troupe troupTmp = enFormTmp.get(cptAFormer);
int cptTroupTmp = 0;
//For the object t in the array of object
for (Troupe t : enFormTmp) {
cptTroupTmp = 0;
//While the object is the same from the one we're working with
while (!troupTmp.equals(t)) {
//Incrementing the previous counter
cptTroupTmp++;
cptAFormer++;
}
And finally here is how I don't really know how to do:
ImageView iView = new ImageView(Optimisation.this);
//To know which background i'll add to the image view
//I'll do this for each different object
if (t.getNom().equals("Barbare"))
iView.setImageResource(R.drawable.barbare);
//...
//The text view with the number of object I found
TextView tView = new TextView(Optimisation.this);
tView.setText("" + cptTroupTmp);
//And now it's here where I want to add the different views
switch (cptCaserne) {
case 0:
caserne1.addView(tView);
caserne1.addView(iView);
case 1:
caserne2.addView(tView);
caserne1.addView(iView);
case 2:
caserne3.addView(tView);
caserne1.addView(iView);
case 3:
caserne4.addView(tView);
caserne1.addView(iView);
}
troupTmp = enFormTmp.get(cptAFormer);
}
}
With this code I have an insane black screen when I'm going on this activity.
Here is what I want to do with in red the textView with the number of objet and in green the imageView of the object...
https://gyazo.com/31b16982ce30b66391bafdd2cd4d86fc
I've found some stuff to do with LayoutInflater but I haven't been successfull with these... Thanks a lot if you could help me.
PS: sorry for the mistakes, I think there are a lot, but in long post like this one, my school english isn't very effective ^^
Thanks again :)
Correct me if I'm wrong. But from what I understood you just need to accomplish what is being shown here https://gyazo.com/31b16982ce30b66391bafdd2cd4d86fc?
Base on your code you just have to add an additional LinearLayout having an horizontal orientation on each LinearLayout before adding the TextView and Image View.
Here's an example using your code:
LinearLayout innerLayout = new LinearLayout(Optimisation.this)
innerLayout.setOrientation(LinearLayout.HORIZONTAL);
ImageView iView = new ImageView(Optimisation.this);
//To know which background i'll add to the image view
//I'll do this for each different object
if (t.getNom().equals("Barbare"))
iView.setImageResource(R.drawable.barbare);
//...
//The text view with the number of object I found
TextView tView = new TextView(Optimisation.this);
tView.setText("" + cptTroupTmp);
innerLayout.addView(tView);
innerLayout.addView(iView);
//And now it's here where I want to add the different views
switch (cptCaserne) {
case 0:
caserne1.addView(innerLayout);
case 1:
caserne2.addView(innerLayout);
case 2:
caserne3.addView(innerLayout);
case 3:
caserne4.addView(innerLayout);
}
Feel free to adjust it if the Layout that I'm adding the innerLayout in is wrong. But basically, that's the idea.

Some layouts expanding slower than others when using LayoutTransition.CHANGING

I have 19 horizontal linearlayouts that are arranged vertically within another linearlayout. They are nearly empty and onclick show text. I am animating the layouts with
parentOne.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
For some reason the first 7 layouts seem to be opening onClick a little slower than the 8th. Only by a split second but I have asked others to look at it when I open it to make sure I'm not crazy. I would post the entire file but it is HUGE and is over the maximum allowable text on this. So here is the meat and potatoes. So, why are my layouts opening slower than others? Numbers 8-19 open instantly onClick and 1-7 have a small delay that is noticeable.
Java On Create
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.tacticalfieldcarestudy);
LinearLayout parentOne = (LinearLayout) findViewById(R.id.TFCLayoutOne);
LinearLayout parentTwo = (LinearLayout) findViewById(R.id.TFCLayoutTwo);
LinearLayout parentTwoBullets = (LinearLayout)findViewById(R.id.TFCLayoutTwoBullets);
LinearLayout parentThree = (LinearLayout)findViewById(R.id.TFCLayoutThree);
LinearLayout parentThreeBullets = (LinearLayout)findViewById(R.id.TFCLayoutThreeBullets);
LinearLayout parentFour = (LinearLayout)findViewById(R.id.TFCLayoutFour);
LinearLayout parentFourBullets = (LinearLayout)findViewById(R.id.TFCLayoutFourBullets);
LinearLayout parentFive = (LinearLayout)findViewById(R.id.TFCLayoutFive);
LinearLayout parentFiveBullets = (LinearLayout)findViewById(R.id.TFCLayoutFiveBullets);
LinearLayout parentSix = (LinearLayout)findViewById(R.id.TFCLayoutSix);
LinearLayout parentSixBullets = (LinearLayout)findViewById(R.id.TFCLayoutSixBullets);
LinearLayout parentSeven = (LinearLayout)findViewById(R.id.TFCLayoutSeven);
LinearLayout parentSevenBullets = (LinearLayout)findViewById(R.id.TFCLayoutSevenBullets);
LinearLayout parentEight = (LinearLayout)findViewById(R.id.TFCLayoutEight);
LinearLayout parentEightBullets = (LinearLayout)findViewById(R.id.TFCLayoutEightBullets);
LinearLayout parentNine = (LinearLayout)findViewById(R.id.TFCLayoutNine);
LinearLayout parentNineBullets = (LinearLayout)findViewById(R.id.TFCLayoutNineBullets);
LinearLayout parentTen = (LinearLayout)findViewById(R.id.TFCLayoutTen);
LinearLayout parentTenBullets = (LinearLayout)findViewById(R.id.TFCLayoutTenBullets);
LinearLayout parentEleven = (LinearLayout)findViewById(R.id.TFCLayoutEleven);
LinearLayout parentTwelve = (LinearLayout)findViewById(R.id.TFCLayoutTwelve);
LinearLayout parentThirteen = (LinearLayout)findViewById(R.id.TFCLayoutThirteen);
LinearLayout parentThirteenBullets = (LinearLayout)findViewById(R.id.TFCLayoutThirteenBullets);
LinearLayout parentFourteen = (LinearLayout)findViewById(R.id.TFCLayoutFourteen);
LinearLayout parentFifteen = (LinearLayout)findViewById(R.id.TFCLayoutFifteen);
LinearLayout parentFifteenBullets = (LinearLayout)findViewById(R.id.TFCLayoutFifteenBullets);
LinearLayout parentSixteen = (LinearLayout)findViewById(R.id.TFCLayoutSixteen);
LinearLayout parentSixteenBullets = (LinearLayout)findViewById(R.id.TFCLayoutSixteenBullets);
LinearLayout parentSeventeen = (LinearLayout)findViewById(R.id.TFCLayoutSeventeen);
LinearLayout parentSeventeenBullets = (LinearLayout)findViewById(R.id.TFCLayoutSeventeenBullets);
LinearLayout parentEighteen = (LinearLayout)findViewById(R.id.TFCLayoutEighteen);
LinearLayout parentEighteenBullets = (LinearLayout)findViewById(R.id.TFCLayoutEighteenBullets);
LinearLayout parentNineteen = (LinearLayout)findViewById(R.id.TFCLayoutNineteen);
LinearLayout parentNineteenBullets = (LinearLayout)findViewById(R.id.TFCLayoutNineteenBullets);
parentOne.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentTwo.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentTwoBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentThree.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentThreeBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFour.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFourBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFive.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFiveBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSix.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSixBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSeven.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSevenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentEight.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentEightBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentNine.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentNineBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentTen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentTenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentEleven.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentTwelve.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentThirteen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentThirteenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFourteen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFifteen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFifteenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSixteen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSixteenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSeventeen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSeventeenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentEighteen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentEighteenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentNineteen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentNineteenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
}
One of 7 Java methods that has unwanted delay onClick
private boolean mOpenOne = false;
public void toggleOne(View view){
TextView stepOne = (TextView)findViewById(R.id.stepOne);
ImageView toggle = (ImageView)findViewById(R.id.toggleStepOne);
if(mOpenOne){
stepOne.setText("Step 1:");
toggle.setImageResource(R.drawable.open);
}
else{
stepOne.setText("1. Casualties with an altered mental status should be disarmed immediately.");
toggle.setImageResource(R.drawable.close);
}
mOpenOne = !mOpenOne;
}
XML with unwanted delay onClick
<LinearLayout
android:id="#+id/TFCLayoutOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
android:animateLayoutChanges="true"
android:onClick="toggleOne"
android:background="#606060"
>
<ImageView
android:id="#+id/toggleStepOne"
android:src="#drawable/open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="#+id/stepOne"
android:text="Step 1:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="0dp"
style="#style/Bullet"
/>
</LinearLayout>
Java for 8th method that works without delay
private boolean mOpenEight = false;
public void toggleEight(View view){
TextView stepEight = (TextView)findViewById(R.id.stepEight);
ImageView toggle = (ImageView)findViewById(R.id.toggleStepEight);
if(mOpenEight){
stepEight.setText("Step 8:");
TextView stepEightBulletOne = (TextView)findViewById(R.id.stepEightBulletOne);
TextView stepEightBulletTwo = (TextView)findViewById(R.id.stepEightBulletTwo);
TextView stepEightBulletThree = (TextView)findViewById(R.id.stepEightBulletThree);
TextView stepEightBulletFour = (TextView)findViewById(R.id.stepEightBulletFour);
TextView stepEightBulletFive = (TextView)findViewById(R.id.stepEightBulletFive);
TextView stepEightBulletSix = (TextView)findViewById(R.id.stepEightBulletSix);
stepEightBulletOne.setText("");
stepEightBulletTwo.setText("");
stepEightBulletThree.setText("");
stepEightBulletFour.setText("");
stepEightBulletFive.setText("");
stepEightBulletSix.setText("");
stepEightBulletOne.getLayoutParams().height = 0;
stepEightBulletTwo.getLayoutParams().height = 0;
stepEightBulletThree.getLayoutParams().height = 0;
stepEightBulletFour.getLayoutParams().height = 0;
stepEightBulletFive.getLayoutParams().height = 0;
stepEightBulletSix.getLayoutParams().height = 0;
toggle.setImageResource(R.drawable.open);
}
else{
stepEight.setText("8. Prevention of hypothermia");
TextView stepEightBulletOne = (TextView)findViewById(R.id.stepEightBulletOne);
TextView stepEightBulletTwo = (TextView)findViewById(R.id.stepEightBulletTwo);
TextView stepEightBulletThree = (TextView)findViewById(R.id.stepEightBulletThree);
TextView stepEightBulletFour = (TextView)findViewById(R.id.stepEightBulletFour);
TextView stepEightBulletFive = (TextView)findViewById(R.id.stepEightBulletFive);
TextView stepEightBulletSix = (TextView)findViewById(R.id.stepEightBulletSix);
stepEightBulletOne.setText("a. Minimize casualty's exposure to the elements. Keep protective gear on or with the casualty if feasible.");
stepEightBulletTwo.setText("b. Replace wet clothing with dry if possible. Get the casualty onto an insulated surface as soon as possible.");
stepEightBulletThree.setText("c. Apply the Ready-Heat Blanket from the Hypothermia Prevention and Management Kit (HPMK) to the casualty's torso (not directly on the skin) and cover the casualty with the Heat-Reflective Shell (HRS).");
stepEightBulletFour.setText("d. If an HRS is not available, the previously recommended combination of the Blizzard Survival Blanket and the Ready Heat blanket may also be used.");
stepEightBulletFive.setText("e. If the items mentioned above are not available, use dry blankets, poncho liners, sleeping bags, or anything that will retain heat and keep the casualty dry.");
stepEightBulletSix.setText("f. Warm fluids are preferred if IV fluids are required.");
stepEightBulletOne.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
stepEightBulletTwo.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
stepEightBulletThree.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
stepEightBulletFour.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
stepEightBulletFive.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
stepEightBulletSix.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
toggle.setImageResource(R.drawable.close);
}
mOpenEight = !mOpenEight;
}
XML of one of the layouts that works without a delay
<LinearLayout
android:id="#+id/TFCLayoutEightBullets"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#A0A0A0"
android:animateLayoutChanges="true"
android:onClick="toggleEight"
>
<LinearLayout
android:id="#+id/TFCLayoutEight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
android:animateLayoutChanges="true"
android:background="#A0A0A0"
>
<ImageView
android:id="#+id/toggleStepEight"
android:src="#drawable/open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="#+id/stepEight"
android:text="Step 8:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="0dp"
style="#style/Bullet"
/>
</LinearLayout>
<TextView
android:id="#+id/stepEightBulletOne"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="#style/BulletIndented"
/>
<TextView
android:id="#+id/stepEightBulletTwo"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="#style/BulletIndented"
/>
<TextView
android:id="#+id/stepEightBulletThree"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="#style/BulletIndented"
/>
<TextView
android:id="#+id/stepEightBulletFour"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="#style/BulletIndented"
/>
<TextView
android:id="#+id/stepEightBulletFive"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="#style/BulletIndented"
/>
<TextView
android:id="#+id/stepEightBulletSix"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="#style/BulletIndented"
/>
</LinearLayout>
Well I figured it out. It looks as though it was a processing issue. If I had to guess I would say that each time I called a method it would search through the giant list of linearlayouts in the onCreate method for the animation to open it.
In my onCreate method I define a CRAP TON of linear layouts as such
LinearLayout parentOne = (LinearLayout) findViewById(R.id.TFCLayoutOne);
parentOne.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
The Fix:
I separated ALL of them and placed them all in their respective onClick methods. Now, when I click them they actually open faster than my other pages that are setup similarly, so I will be changing those as well. My java methods were:
private boolean mOpenOne = false;
public void toggleOne(View view){
TextView stepOne = (TextView)findViewById(R.id.stepOne);
ImageView toggle = (ImageView)findViewById(R.id.toggleStepOne);
if(mOpenOne){
stepOne.setText("Step 1:");
toggle.setImageResource(R.drawable.open);
}
else{
stepOne.setText("1. Casualties with an altered mental status should be disarmed immediately.");
toggle.setImageResource(R.drawable.close);
}
mOpenOne = !mOpenOne;
}
I have changed them to:
private boolean mOpenOne = false;
public void toggleOne(View view){
TextView stepOne = (TextView)findViewById(R.id.stepOne);
ImageView toggle = (ImageView)findViewById(R.id.toggleStepOne);
LinearLayout parentOne = (LinearLayout) findViewById(R.id.TFCLayoutOne);
parentOne.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
if(mOpenOne){
stepOne.setText("Step 1:");
toggle.setImageResource(R.drawable.open);
}
else{
stepOne.setText("1. Casualties with an altered mental status should be disarmed immediately.");
toggle.setImageResource(R.drawable.close);
}
mOpenOne = !mOpenOne;
}
Hope this helps others!

In Android get center of Relative Layout on API 10

I'm trying to find the center coordinates of a RelativeLayout that for the moment, is empty. I need to find the center before I can put anything into the Relative Layout. The Relative Layout is not the whole screen, so I can't use metrics to find the screen dimensions. Since my minimum API is 10, I can't use rLayout.getX() or rLayout.getY(). So what I've tried is:
int xScreenInt = gBoard_RL.getWidth()/2;
int yScreenInt = gBoard_RL.getHeight()/2;
int xInt = gBoard_RL.getLeft() + xScreenInt;
int yInt = gBoard_RL.getTop() + yScreenInt;
and all I get is 0,0. Does anyone have an any ideas how I can get this done? Here's my layout and the Activity so far:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/gb1_root_RL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".GB1" >
<RelativeLayout
android:id="#+id/gb1_topInfo_RL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#drawable/blu_grade_bg"
android:orientation="vertical"
android:paddingBottom="15dp" >
<TextView
android:id="#+id/gb1_scValue_TV"
style="#style/mainScoreValueStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/gb1_score_TV"
android:layout_marginLeft="10dp" />
<TextView
android:id="#+id/gb1_timeValue_TV"
style="#style/mainTimeValueStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/gb1_time_TV"
android:layout_marginRight="10dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/gb1_gf_RL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/gb1_topInfo_RL"
android:padding="10dp" >
</RelativeLayout>
the Relative Layout I need to find the center of is: android:id="#+id/gb1_gf_RL"
public class GB1 extends Activity {
TextView GScore_TV;
RelativeLayout gBoard_RL;
int xScreenInt, yScreenInt, xInt, yInt;
String xStr, yStr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.cutin, R.anim.cutout);
setContentView(R.layout.gb_layout);
GScore_TV = (TextView) findViewById(R.id.G1_scoreValue_TV);
gBoard_RL = (RelativeLayout) findViewById(R.id.gb1_gf_RL);
gBoard_RL.setBackgroundColor(Color.MAGENTA);
xScreenInt = gBoard_RL.getWidth()/2;
yScreenInt = gBoard_RL.getHeight()/2;
xInt = gBoard_RL.getLeft() + xScreenInt;
yInt = gBoard_RL.getTop() + yScreenInt;
xStr = String.valueOf(xInt);
yStr = String.valueOf(yInt);
GScore_TV.setText(xStr + ", " + yStr);
}
}
Try putting your code in a onGlobalLayout listener:
myRelativelayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
this.layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//Your code here
}
});
EDIT:
Since, as #xorgate pointed out, the layout isnt drawn yet in the onCreate method. The onGlobalLayout method will be called when the layout is drawn and calculations such as yours should be done in there.
onCreate is called before a layout pass is done. All Views will have no size yet.
Try setting a global layout listener, and do the positioning code there.

Move two side by side textviews to be one under another if text is too long

I have two textviews like this:
=======================
= TextView1 TextView2 =
=======================
And I would like to detect when the textviews are too long such that they are displayed like this:
=======================
= TextView1 =
= TextView2 =
=======================
currently for longer text, it is displayed like this:
=======================
= TextView1 Text =
= View2 =
=======================
how can I do this, such that when the text is short the textviews are side by side and when it is too long, the second textview is not splitted but moved to the second line?
I tought at a solution to create a single textview and build the text according to length (text 1 + padding + text 2 if short, and text 1 + "\n" + text 2 if long) but I do not like this solution.
Is there any way to detect if the second text will be split such that to change the orientation of the layout that contains the textviews from horizontal cu vertical?
UPDATE
This is my xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView
android:id="#+id/my_text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="10dp"
android:text="#string/text1"/>
<TextView
android:id="#+id/my_text_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"/>
</LinearLayout>
I have found a better solution. Changed my textviews into autoresizable textviews (more info here)
Also, each textview is in a separate layout, to make sure both textviews are resized to the same value.
My xml looks like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/value_linear_layout"
android:gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content">
<com.mihaela.view.AutoResizeTextView
android:id="#+id/my_text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content">
<com.mihaela.view.AutoResizeTextView
android:id="#+id/my_text_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
and I have implemented the OnTextResizeListener from AutoResizeTextView to do this:
public class TextWidthResizeListener implements OnTextResizeListener {
#Override
public void onTextResize(TextView textView, float oldSize, float newSize) {
TextPaint paint = textView.getPaint();
if (paint.measureText(textView.getText().toString()) > (valueLinearLayout.getWidth() / 2)){
valueLinearLayout.setOrientation(LinearLayout.VERTICAL);
}
}
}
where valueLinearLayout is:
valueLinearLayout = (LinearLayout)findViewById(R.id.value_linear_layout);
This solution best fits for me, as the textviews are dimensioned when they are side by side until a minimum size. When the minimum size is reached, and the text still does not fit, the textviews will be aligned one under another.
Also, this idea with the listener can be applied to non-resizable textviews also.
I will set this answer as the correct one.
You should use a single, multi-line TextView and set the text as follows :
mTextView.setText(text1+" "+text2);
or
mTextView.setText(text1+"\n"+text2);
depending on your particular needs.
EDIT: you could specify your text in html, and then use Html.fromHtml(htmlString) and display this text in your TextView.
String text1 ="<font color=\"red\">This is some text!</font>"
String text2="<font color=\"blue\">This is some other text!</font>"
textView.setText(Html.fromHtml(text1+ "<br/>"+ text2);
I made a slightly different version of the accepted answer. I did not alter my layout xml in any way and did not use onTextResize() or AutoResizeTextView as that seemed an overkill for my situation. I needed my LinearLayout to switch from Horizontal orientation to Vertical orientation if the device's language setting caused a long string to be used.
Layout
<LinearLayout
android:id="#+id/customer_care_bottom_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:layout_marginBottom="#dimen/lmargin_bottom_10">
<TextView
android:id="#+id/customer_care_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/CUSTOMER_CARE_TITLE" />
<TextView
android:id="#+id/customer_care_number_information"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/CUSTOMER_CARE_INFORMATION"/>
</LinearLayout>
Java
private void setCustomerServiceLayoutOrientationBasedOnTextWidth() {
TextPaint paint = customer_care_number_text.getPaint();
TextView tvCustomerCareTitle = (TextView) findViewById(R.id.customer_care_title);
TextView tvCustomerCareInformation = (TextView) findViewById(R.id.customer_care_information);
int halfCustomerServiceLayoutWidth = getScreenWidth() / 2;
boolean isCustomerCareTitleTooLong = paint.measureText(tvCustomerCareTitle.getText().toString()) > customerServiceLayoutWidth;
boolean isCustomerCareInformationTooLong = paint.measureText(tvCustomerCareInformation.getText().toString) > customerServiceLayoutWidth;
if (isCustomerCareTitleTooLong || isCustomerCareInformationTooLong) {
LinearLayout llCustomerCareBottom = (LinearLayout) findViewById(R.id.customer_care_bottom_layout);
llCustomerCareBottom.setOrientation(LinearLayout.VERTICAL);
}
}
private int getScreenWidth() {
int screenWidth;Display display = getWindowManager().getDefaultDisplay();
if (Build.VERSION.SDK_INT < 13) {
screenWidth = display.getWidth();
} else {
Point point = new Point();
display.getSize(point);
screenWidth = point.x;
}
return screenWidth;
}

Categories

Resources