I am developing an Android quiz game.
When a user replies to the first question I open this dialog to show its progress:
This is the layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/txtLevel1"
android:layout_alignTop="#+id/txtLevel1"
android:layout_centerInParent="true"
android:background="#color/orange" />
<TextView
android:id="#+id/txtLevel3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5.000" />
<TextView
android:id="#+id/txtLevel2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtLevel3"
android:text="2.500"/>
<TextView
android:id="#+id/txtLevel1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtLevel2"
android:text="1.000"/>
</RelativeLayout>
Is there any way to move the View element (the orange box) from txtLevel1 (1.000 points) to txtLevel2 (2.500 points) maybe with an animation/transition and not statically?
You could try this to move the orange box from txtLevel1 to txtLevel2:
View orangeBox = getActivity().findViewById(R.id.orange_box);
TextView tv = getActivity().findViewById(R.id.txtLevel2);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(300, RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.ALIGN_BOTTOM, tv.getId());
p.addRule(RelativeLayout.ALIGN_TOP, tv.getId());
orangeBox.setLayoutParams(p);
For the animation this might help you.
Related
I have the following Horizontal Linear Layout inside a Vertical Linear Layout :
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="#color/White"
android:orientation="vertical">
<TextView
android:id="#+id/pokemonTeambuilderTitleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|start"
android:paddingStart="10dp"
android:paddingTop="10dp"
android:paddingEnd="10dp"
android:paddingBottom="5dp"
android:text="#string/placeholder"
android:textColor="#color/Black"
android:textSize="16sp"
android:textStyle="bold" />
<LinearLayout
android:id="#+id/pokemonTeambuilderSpritesLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
And programmatically I set the elements this way in my adapter :
for (Pokemon pokemon : pokemonTeam.getPokemonList()) {
String pokemonNickname = pokemon.getNickname();
String pokemonName = pokemon.getName();
ImageView tvPokemonSprite = new ImageView(mContext);
int color = PokemonUtils.getDominantColorFromPokemon(pokemon.get_id(),mContext);
tvPokemonSprite.setImageResource(PokemonUtils.getPokemonSugimoriImageById(pokemon.get_id(), mContext));
tvPokemonSprite.setScaleType(ImageView.ScaleType.FIT_CENTER);
PokemonUtils.setResourceAndBorderBackgroundColorToElementFromColor(tvPokemonSprite,R.drawable.circle_shape,color,PokemonUtils.lighterColor(color, DARK_FACTOR));
TableLayout.LayoutParams layout = new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
tvPokemonSprite.setLayoutParams(layout);
holder.teamSpritesLinearLayout.addView(tvPokemonSprite);
}
However , if I rotate my phone , the elements don't fit completly in the Linear Layout , the bottom and the top is half cutted , also if the horizontal linear layout only have 1 Pokemon , instead of adapting to 1 slot it expands the WHOLE view (as you can see with charizard)
Add below code to imageview:
tvPokemonSprite.setAdjustViewBounds(true)
i hope work for you.
I'm kind of stuck with a problem and I hope you can help me. Im developing an Android application with Android Studio.
I have a TableLayout with 2 elements. The first column is a LinearLayout with a percentage painted in the background, and the second column, just a simple textview (for the example, I put two rows in it).
I have solved the problem with XML representation, and I need to do the same but programmatically. Here is my XML:
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id = "#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/orionBackground"
android:orientation="horizontal"
android:weightSum="100">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="30"
android:background="#color/orionRed">
<TextView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text=""
android:layout_gravity="right"
android:textColor="#color/orionWhite"/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:layout_gravity="right"
android:background="#color/orionBackground"
android:textColor="#color/orionWhite"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/orionBackground"
android:orientation="horizontal"
android:weightSum="100">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:background="#color/orionRed">
<TextView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text=""
android:textColor="#color/orionWhite"/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="There"
android:layout_gravity="right"
android:background="#color/orionBackground"
android:textColor="#color/orionWhite"/>
</TableRow>
</TableLayout>
I need to do the same but programmatically and I don't know how could be wrong with what Im doing. Here is my code:
TableLayout table = (TableLayout)findViewById(R.id.tableLayout);
/*TextView*/
TextView textViewHelloWorld = new TextView(getBaseContext());
textViewHelloWorld.setText("");
textViewHelloWorld.setLayoutParams(
new ViewGroup.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT));
textViewHelloWorld.setTextColor(Color.parseColor("#FFFFFF"));
/*Child Layout*/
LinearLayout linearLayout = new LinearLayout(getBaseContext());
/*Here I set the percentage of the LinearLayout to 10*/
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
0, ViewGroup.LayoutParams.MATCH_PARENT, 10f);
linearLayout.setLayoutParams(params1);
linearLayout.setBackgroundColor(Color.parseColor("#A23934"));
/*Textbox Added | I tested before the above line*/
linearLayout.addView(textViewHelloWorld);
/*Parent Layout*/
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,100f);
LinearLayout parentLayout = new LinearLayout(getBaseContext());
parentLayout.setOrientation(LinearLayout.HORIZONTAL);
/*Finally, I added the LinearLayout into de parent LinearLayout*/
parentLayout.addView(linearLayout,params);
TableRow rowHeader = new TableRow(getBaseContext());
TableRow.LayoutParams params10 = new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
rowHeader.setLayoutParams(params10);
/*Then, the linear layout is Added into de TableRow*/
rowHeader.addView(parentLayout);
/*And added to the row*/
table.addView(rowHeader);
Visually, this is how I need to see it:
What I want to accomplish
This is for an app that will show the percentage of amount in a order book of cryptocurrencies.
My issue is that my solution print the whole background and should be print only a part of the background. The image above, prints the percentage, 30 and 60, defined by the layout_weight attribute, but when I do it programmatically doesnt work at all.
Why isnt working?
I really hope you can help me with this.
Thanks a lot
try to set elevation to TextView like ViewCompat.setElevation(textViewHelloWorld, 10);
so this be like
TextView textViewHelloWorld = new TextView(getBaseContext());
textViewHelloWorld.setText("sample text");
textViewHelloWorld.setLayoutParams(
new ViewGroup.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT));
textViewHelloWorld.setTextColor(Color.parseColor("#FFFFFF"));
ViewCompat.setElevation(textViewHelloWorld, 10);
Background: I'm making an application that can creates timed based alarms, location based or both and in the activity that creates the alarm I have checkbox for each- timepicker and map and you uncheck it if you don't want one of these features in your alarm. Here's a picture of this activity-http://prntscr.com/dr1s74 .
If you uncheck the map checkbox it removes the fragment and sets layout params of the radioGroup to below the title (because the map is gone now), center it and to add margin.
While addRule(RelativeLayout.BELOW, id) works, both setMargins(left, top, right, bottom) and addRule(RelativeLayout.CENTER_HORIZONTAL) doesn't work and because of that the radioGroup is off center and without the correct space below the title.
XML Activity:
Description of the possible problem: The only reason I can think it happens is that I have CoordinatorLayout as the parent layout (because its interaction between FAB and SnackBar) and ScrollView inside it containing RelativeLayout (Open to suggestions if you have a better way). And that for some reason makes problems when trying to add rules to layout params.
XML Code (Removed the radioButtons and CheckBox for easier viewing):
<android.support.design.widget.FloatingActionButton
android:id="#+id/saveAlarm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="#dimen/fab_margin"
android:layout_marginRight="#dimen/fab_margin"
android:onClick="fabClicked"
app:srcCompat="#android:drawable/ic_menu_save" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relative_layout_alarm_creator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.rome.locationalarm.AlarmCreator">
<EditText
android:id="#+id/alarmTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:hint="Alarm title" />
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_below="#+id/alarmTitle"
android:layout_marginTop="10dp"
tools:context="com.example.rome.locationalarm.MapsFragment" />
<com.example.rome.locationalarm.CustomTimePicker
android:id="#+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/days"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/chooseLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/map"
android:layout_centerHorizontal="true"
android:text="Choose Location" />
<RadioGroup
android:id="#+id/days"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/map"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:orientation="horizontal">
</RadioGroup>
</RelativeLayout>
</ScrollView>
JAVA Code (Only the important part):
if(locationCheckBox.isChecked()){
//If user wants location
RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.BELOW, mapFragment.getId());
//All of these don't work
layoutParams.setMargins(0, 10, 0, 0); // (left, top, right, bottom)
//All of these are tries to center the radioGroup
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.removeView(radioGroup);
layout.addView(radioGroup, layoutParams);
//layout.updateViewLayout(radioGroup, layoutParams); doesn't work as well.
Thank you for taking your time trying to help, have a nice year!
you need to do this to get the LayoutParams
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) getLayoutParams();
So I've checked the other questions to hide a progress bar but all seem to suggest doing what I'm already doing.
I'm trying to use
mProductListProgressBar.setVisibility(View.GONE);
and I'm finding it by
mProductListProgressBar = (ProgressBar) mRoot.findViewById(R.id.product_list_progressbar);
I know it's the correct progress bar as I can move it around the screen before with various LayoutParams commands. But it won't hide.
The current code I have (including the moving of the progress bar) is
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
mProductListProgressBar.setLayoutParams(params);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
mProductListProgressBar.setLayoutParams(params);
mProductListProgressBar.setVisibility(View.GONE);
mProductListErrorTextView.setVisibility(View.VISIBLE);
mProductListErrorTextView.setText(errorMessage);
The progress bar moves to left and bottom but is still visible. I have tried View.INVISIBLE as well as View.GONE but neither work.
It's driving me nuts!
Thanks
UPDATE 1
protected void showError(int errorMessage){
/*RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
mProductListProgressBar.setLayoutParams(params);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
mProductListProgressBar.setLayoutParams(params);
*/
mProductListProgressBar.setVisibility(View.GONE);
mProductListErrorTextView.setVisibility(View.VISIBLE);
mProductListErrorTextView.setText(errorMessage);
}
and calling it with
showError(R.string.wish_list_empty);
UPDATE 2
xml of fragment
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="#+id/product_list_gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:horizontalSpacing="#dimen/standardMargin"
android:listSelector="#android:color/transparent"
android:numColumns="#integer/columns"
android:scrollbarStyle="outsideOverlay"
android:verticalSpacing="#dimen/standardMargin" />
<RelativeLayout
android:id="#+id/product_list_header"
android:layout_width="match_parent"
android:layout_height="#dimen/refineHeaderHeight"
android:background="#drawable/product_list_header"
android:visibility="gone" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:padding="10dp"
android:src="#drawable/dropdown_image" />
<TextView
android:id="#+id/product_list_header_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center|left"
android:paddingLeft="10dp"
android:textAllCaps="true"
android:textColor="#android:color/black"
android:textIsSelectable="false"
android:textSize="#dimen/standardTextSize" />
<TextView
android:id="#+id/product_list_refine_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/button"
android:clickable="true"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:paddingRight="30dp"
android:text="#string/refine"
android:textAllCaps="true"
android:textColor="#838383"
android:textSize="#dimen/standardTextSize" />
</RelativeLayout>
<LinearLayout
android:id="#+id/product_list_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:background="#drawable/product_list_footer"
android:gravity="center"
android:orientation="horizontal"
android:padding="15dp"
android:visibility="gone" >
<ProgressBar
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="center"
android:layout_marginRight="10dp"
android:background="#android:color/transparent"
android:gravity="center" />
<TextView
android:id="#+id/product_list_footer_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/loading_message"
android:textAllCaps="true"
android:textSize="#dimen/standardTextSize" />
</LinearLayout>
<ProgressBar
android:id="#+id/product_list_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone"/>
<TextView
android:id="#+id/product_list_error_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textAllCaps="true"
android:textSize="#dimen/standardTextSize"
android:visibility="gone" />
<TextView
android:id="#+id/debug"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#aa000000"
android:gravity="left|center_vertical"
android:minLines="2"
android:padding="10dp"
android:textColor="#android:color/white"
android:visibility="gone" />
</RelativeLayout>
The problem with this behaviour usually happens when your view which you try to hide is needed / referenced by someone else.
In your case, as you mentioned in the comments, you use mProductListProgressBar for setEmptyView() of your GridView.
Another possibility of running into similar troubles when there other views in your relative container layout which set their position relatively to your mProductListProgressBar. This setting could be either in the code or in .xml.
Please make sure you don't have any of above and View.GONE should work fine.
As for setEmptyView() - it is only used to show something meaningful to the user when your adapter is empty. I recommend just setting up simple Layout for that with, say, TextView "no items", in the middle, and pass it to setEmptyView()
Hope that helps.
I know that it is an old question. But I just spent a lot of time debugging similar situation. I could not hide ProgressBar on top of unityPlayer view.
One more thing to ensure is that You are hiding it while in UI thread.
To ensure surround Your UI code with:
runOnUiThread(new Runnable() {
#Override
public void run() {
loadingIndicator.setVisibility(View.GONE);
}
});
A solution that worked for me was to hide the view in XML:
android:visibility="gone"
and then unhide/hide it at runtime when needed:
yourProgressBar.setVisibility(View.VISIBLE) // or View.VISIBLE depending on situation
I don't know how your code is written but had similar problem and resolved by removing the view object from declaration
like
mProductListProgressBar = (ProgressBar) mRoot.findViewById(R.id.product_list_progressbar);
should be
mProductListProgressBar = (ProgressBar) findViewById(R.id.product_list_progressbar);
Try it and check
Reason is the progress bar is on the top of every view in the hierarchy and it's not belong to any root view.
This is what is expected to come-----
The first text view:
Fox Pro
Drag
Main View Item
This is what is coming-----
And the other text view:
Fox Pro
Drag
Main View Item
So if the text is long enough to certain limit, it is coming wrapped in a next line.
Please note that the problem is only coming in the higher ppi 7inch device(~213 ppi).
The same thing is coming fine in lower ppi 7inch device(~160 ppi).
I tried to do the following programatically,
setting the width to full match_parent-------
DisplayMetrics dm = new DisplayMetrics();
// Display device dpi value of Y in pixels
int screenDPIy = (int)dm.ydpi;
if(screenDPIy > 180)
{
entrytype.setTextSize(14);
entrydate.setTextSize(12);
entrytype.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
entrydate.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
}
else
{
entrytype.setTextSize(16);
entrydate.setTextSize(14);
}
No Success.
My Layout xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/mapentrieslist"
android:layout_width="match_parent"
android:layout_height="58dp"
android:background="#drawable/selector"
android:gravity="left|center_vertical"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="4dp"
android:layout_weight="0.9"
android:orientation="horizontal"
android:padding="5dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.47"
android:orientation="vertical" >
<TextView
android:id="#+id/entrytype"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fox Pro"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/vehicle_source"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:visibility="gone"/>
<TextView
android:id="#+id/entrydate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#989898"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
You have a number of options:
Add a marquee that automatically scrolls : textView marquee
Place the TextView inside of a Horizontal ScrollView instead of a LinearLayout
Set the TextView to put an ellipses at the end : Detect whether TextView in ListView is ellipsized
The first two preserve the text while the third removes the extra characters.