Need to make a gridview with two static text fields - java

Hello i need to make a gridview like the following images with static textviews.
I need to make it in Portrait mode. I know there are lot of guides available on stackoverflow on how to make a gridview with two textviews but i am unable to follow them and failed many times trying to create a gridview exactly like this. Instead of marking my post as negative if someone could help me build a gridview like above i will be really great full.
Thanks!
Edit: I have tried a TableLayout in order to achieve the above layout and things are going well except i cannot draw a vertical line(divider). See the image below:
My Code:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:divider="#FF909090"
android:orientation="horizontal"
android:stretchColumns="1"
android:background="#drawable/custom_background"
android:showDividers="middle">
<TableRow>
<TextView
android:text="Name"
android:padding="3dip" />
<TextView
android:text="Imran"
android:gravity="left"
android:padding="3dip" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TableRow>
<TextView
android:text="Name"
android:padding="3dip" />
<TextView
android:text="Test"
android:gravity="left"
android:padding="3dip" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TableRow>
<TextView
android:text="Name"
android:padding="3dip" />
<TextView
android:text="Test"
android:gravity="left"
android:padding="3dip" />
</TableRow>
Thanks to all for helping me out.

I think your problem is in the assumption that you need to use a GridView here.
What you presented should be done with a ListView or, even better, a RecyclerView.
Try looking at each row as a list item and the task will become really easy :-)
Like this:

Related

Can't fully hide android material card background

Image how it looks
Can't fully hide android material card background. I have searched and watched all videos available but nothing has worked so far. I know I could just put something to fill the card but I want to know just in case.
The code:
<com.google.android.material.card.MaterialCardView
android:id="#+id/card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#40000000"
android:clickable="true"
app:cardCornerRadius="30dp"
android:layout_margin="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Planner"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="20sp"
android:fontFamily="#font/nova_bold"/>
<ImageView
android:layout_width="150dp"
android:layout_height="130dp"
android:layout_marginStart="10dp"
android:layout_marginTop="30dp"
android:padding="1dp"
app:srcCompat="#drawable/ic_plan_vec" />
</com.google.android.material.card.MaterialCardView>
please use in your XML app:cardBackgroundColor="#color/yourColor"
or in your java code card.setCardBackgroundColor(YourColor); instead of the default setBackgroundColor()
UPDATE
for the opacity change android:backgroundTint="#40000000" by android:alpha="0.4"

"Include Other Layout" causes build to fail in emulator. No errors or associated warnings

I've added a couple small relative layouts to the main layout of an activity using "Include Other Layout" in the graphical editor. However, for some reason when I try to load the activity in the emulator, it fails. There are no warnings other than a bunch of generic stuff in LogCat that looks exactly the same as any other time I have an error. In the .Java file for this activity, I've successfully made reference to the views within these included layouts without any problems.
This are the elements from the activity's .xml file that include the layouts.
<include
android:id="#+id/include1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/lblParklandAssistDesc"
android:layout_below="#+id/lblParklandAssistDesc"
layout="#layout/parklandweight" />
<include
android:id="#+id/include2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/include1"
android:layout_below="#+id/include1"
layout="#layout/parklandbsa" />
And these are the layouts that are included
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btnWeightUp"
style="?android:attr/buttonStyleSmall"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/Up" />
<Button
android:id="#+id/brnWeightDown"
style="?android:attr/buttonStyleSmall"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/btnWeightUp"
android:text="#string/Down" />
<TextView
android:id="#+id/lblParklandWeightTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/btnWeightUp"
android:text="#string/lblWeight"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/txtWeight"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:layout_below="#+id/lblParklandWeightTitle"
android:layout_toRightOf="#+id/btnWeightUp"
android:ems="10"
android:inputType="number"
android:text="100" >
<requestFocus />
</EditText>
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/lblParklandWeightTitle" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/lb" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/kg" />
</RadioGroup>
</RelativeLayout>
Then the other one.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btnBSADown"
style="?android:attr/buttonStyleSmall"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/btnBSAUp"
android:layout_below="#+id/btnBSAUp"
android:text="#string/Down" />
<TextView
android:id="#+id/lblParklandBSAtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="58dp"
android:text="#string/lblParklandBSAtitle"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/btnBSAUp"
style="?android:attr/buttonStyleSmall"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/Up" />
<EditText
android:id="#+id/txtBSA"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/lblParklandBSAtitle"
android:layout_below="#+id/lblParklandBSAtitle"
android:ems="10"
android:inputType="number"
android:text="3" />
</RelativeLayout>
I don't have the foggiest idea as to what could be going on or frankly even where to look. I've been working with eclipse and java with android for almost two weeks, have made one app, and have been having great luck and easy learning with everything. This has left me stumped. Any help would be greatly appreciated!
New Addition(added .Java Content because it seems that the xml layouts themselves may not be the problem):
In the top of the activity's class I declare the variables that will handle the other layouts like this:
View parklandWeight;
View parklandBSA;
Then, when I assign the object to it - I do so like this:
parklandWeight = (View) findViewById(R.layout.parklandweight);
parklandBSA = (View) findViewById(R.layout.parklandbsa);
The only error that appears to be from this activity.java in the LogCat occurs just after this second assignment. On another assignment that refers a button inside one of these layouts:
btnWeightUp = (Button) parklandWeight.findViewById(R.id.btnWeightUp);
Could this be the root of my issue? I've read at least five or six different threads here in StackOverflow where this is what is advised to do. Thanks in advance.
I have tried these code runs perfect... Try these.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="HardCodedText" >
<include
android:id="#+id/include2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/include1"
android:layout_alignParentTop="true"
android:layout_below="#+id/include1"
layout="#layout/top" />
<Button
android:id="#+id/btnWeightUp"
style="?android:attr/buttonStyleSmall"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/include2"
android:text="Up" />
<Button
android:id="#+id/brnWeightDown"
style="?android:attr/buttonStyleSmall"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:layout_below="#+id/btnWeightUp"
android:text="Down" />
<TextView
android:id="#+id/lblParklandWeightTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/include2"
android:layout_toRightOf="#+id/btnWeightUp"
android:text="lblWeight"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/txtWeight"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:layout_below="#+id/lblParklandWeightTitle"
android:layout_toRightOf="#+id/btnWeightUp"
android:ems="10"
android:inputType="number"
android:text="100" >
<requestFocus />
</EditText>
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/include2"
android:layout_toRightOf="#+id/lblParklandWeightTitle" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="lb" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="kg" />
</RadioGroup>
</RelativeLayout>
I was typing the variables for the layouts wrong. Instead of using
View layoutVariable;
I needed to be typing by the actual layout type as below.
RelativeLayout parklandWeight;
RelativeLayout parklandBSA;
Then, when assigning the actual layout object to the variable, I should have been typing it as the layout type, of course. But, more than that, in the findViewById method I was using R.layout.layout_id when I should have been using R.id.layout_id. I was also referring to the xml file name where I should have been referring to the same +id that is used in the include tag in the xml file.
This is the way I should have been doing it:
parklandWeight = (RelativeLayout) findViewById(R.id.include1);
parklandBSA = (RelativeLayout) findViewById(R.id.include2);
I hope this helps any newcomers to android development who run into the same problem. I hope you all have an incredible day!

TextView not scrolling Smoothly

I am trying to scroll my textview using the following code:
TextView text = (TextView) findViewById(R.id.textView12);
text.setMovementMethod(ScrollingMovementMethod.getInstance());
int height = getResources().getDrawable(R.drawable.pic_ground_pranic).getIntrinsicHeight();
text.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, height));
text.setText("As part of an Android App I am building a button set.For performance reasons and to keep the code simpler, the Android system uses pixels as the standard unit for expressing dimension or coordinate values. That means that the dimensions of a view are always expressed in the code using pixels, but always based on the current screen density. For instance, if myView.getWidth() returns 10, the view is 10 pixels wide on the current screen, but on a device with a higher density screen, the value returned might be 15. If you use pixel values in your application code to work with bitmaps that are not pre-scaled for the current screen density, you might need to scale the pixel values that you use in your code If you need to control exactly how your application will look on various screen configurations, adjust your layouts and bitmap drawables in configuration-specific resource directories. For example, consider an icon that you want to display on medium and high density screens. Simply create your icon at two different sizes (for instance 100x100 for medium density and 150x150 for high density) and put the two variations in the appropriate directories, using the proper qualifiers: For instance, suppose a device has a WVGA high-density screen, which is 480x800 and about the same size as a traditional HVGA screen, but it's running an application that has disabled pre-scaling. In this case, the system will lie to the application when it queries for screen dimensions, and report 320x533 (the approximate mdpi translation for the screen density). Then, when the application does drawing operations, such as invalidating the rectangle from (10,10) to (100, 100), the system transforms the coordinates by scaling them the appropriate amount, and actually invalidate the region (15,15) to (150, 150). This discrepancy may cause unexpected behavior if your application directly manipulates the scaled bitmap, but this is considered a reasonable trade-off to keep the performance of applications as good as possible. If you encounter this situation, read Based on the density of the current screen, the system uses any size- or density-specific resources from your application and displays them without scaling. If resources are not available in the correct density, the system loads the default resources and scales them up or down as needed to match the current screen's density. The system assumes that default resources (those from a directory without configuration qualifiers) are designed for the baseline screen density (mdpi), unless they are loaded from a density-specific resource directory. Pre-scaling is, thus, what the system does when resizing a bitmap to the appropriate size for the current screen density. to match the un-scaled bitmap source. The buttons are part of a nested set of LinearLayouts.The question then is: How to resize the layout.I have tried several suggested techniques, and none come close to working. Here is a subset of the XML that builds the button set:");
All is working fine but my text view is not scrolling smoothly. I thought I have to put my Textview inside a Scrollview but this is also not working.
Does it happen because I am resizing my textview?
Please provide any solution for this problem. I already spent a lot of time on this.
My xml layout file is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/include1"
android:layout_centerHorizontal="true"
android:background="#android:color/white"
android:scrollbarStyle="insideInset" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="14dp"
android:text="Text 1"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView2"
android:layout_marginLeft="25dp"
android:layout_toRightOf="#+id/textView2"
android:text="Text 2"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView3"
android:layout_marginLeft="25dp"
android:layout_toRightOf="#+id/textView3"
android:text="Text 3"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView4"
android:layout_marginLeft="25dp"
android:layout_toRightOf="#+id/textView4"
android:text="Text 4"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:layout_marginLeft="5dp"
android:layout_marginTop="3dp"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_below="#+id/textView3"
android:layout_marginTop="3dp"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_below="#+id/textView4"
android:layout_marginTop="3dp"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView5"
android:layout_below="#+id/textView5"
android:layout_marginTop="3dp"
android:textColor="#android:color/black" />
</RelativeLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/relativeLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0" >
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:soundEffectsEnabled="true" />
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Volume 1"
android:textColor="#android:color/black" />
</RelativeLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/relativeLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0" >
<SeekBar
android:id="#+id/seekBar2"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:soundEffectsEnabled="true" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Volume 2"
android:textColor="#android:color/black" />
</RelativeLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Save Volumes" />
</LinearLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/frameLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:orientation="vertical" >
<TextView
android:id="#+id/textView12"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical|center_horizontal"
android:gravity="top|left"
android:scrollbars="vertical"
android:visibility="visible" />
</FrameLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow8"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<FrameLayout
android:id="#+id/frameLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center" />
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_marginRight="30dp"
android:clickable="true"
android:text="Detailed Instruction" />
</FrameLayout>
</TableRow>
</TableLayout>
</ScrollView>
</RelativeLayout>
Thanks in advance!
In my case I just needed to put my TextView inside a ScrollView. Scrolling got very smooth and users lived happily every after!
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
>
<TextView
android:id="#+id/textview_about"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:textSize="20dp" />
</ScrollView>
problem is ScrollView inside ScrollView.
When you put ScrollView inside ScrollView android just get confused which scroll view is touched. So sometimes it gets unable to deliver touch event.
After searching for a time I found a link or blog that will extract this problem and also have solution.
Take a look at this blog. This is exactly what you have been searching for and i am sure that it will solve your problem.
Now time to Enjoy ;)
The TextView scroll only if you restrict its height. Simple rule is when the text is more then the available space then textview start scrolling.
One way is what you are following by assigning a fixed height which you got from some image size. One other way is by setting the maximum number of lines.
TextView text = (TextView) findViewById(R.id.textView12);
text.setMaxLines(5);
text.setMovementMethod(ScrollingMovementMethod.getInstance());
you can even make it more dynamic. Simply by calculating number of lines.
Number of lines = (image Height in pixels)/(text font size in pixel)
First of all there is no need of the setting the Layoutparams as because you have give it in to the xml layout.
And if you have such long string then dont put it directly on java code. Instead of it put that string to the resources directory.
go to values, and open strings.xml file and define your string.
See below:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelperStackOverflowActivity!</string>
<string name="app_name">HelperStackOverflow</string>
<string name="String1">"PUT YOUR STRING HERE" </string>
</resources>
After doing that set the string in Java code whenever you want.
as like:
text = (TextView)findViewById(R.id.textView12);
text.setText(getResources().getString(R.string.String1));
it Scroll nice as normal scrolling.
Hope it will help you.
Enjoy. :)

align text center with android

I know it sounds easy. I need to put a text in center, but when the text is too long it needs to go below, but still align in the center of my xml.
Here's my code :
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/showdescriptioncontenttitle"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_centerHorizontal="true"
>
<TextView
android:id="#+id/showdescriptiontitle"
android:text="Title"
android:textSize="35dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
I put paddingTop and Bottom because I need some space.
PS: My code is bigger; it's in a RelativeLayout.
Set also android:gravity parameter in TextView to center.
For testing the effects of different layout parameters I recommend to use different background color for every element, so you can see how your layout changes with parameters like gravity, layout_gravity or others.
use this way
txt.setGravity(Gravity.CENTER);
use this way in xml
<TextView
android:id="#+id/myText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Time is precious, so love now."
android:gravity="center"
android:textSize="30dp"
android:textColor="#fff"
/>
You can use the following:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/showdescriptioncontenttitle"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<TextView
android:id="#+id/textview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Your text"
android:typeface="serif" />
</LinearLayout>
The layout needs to be relative for the "center" property to be used.
Adding android:gravity="center" in your TextView will do the trick (be the parent layout is Relative/Linear)!
Also, you should avoid using dp for font size. Use sp instead.
Just add these 2 lines;
android:gravity="center_horizontal"
android:textAlignment="center"
add layout_gravity and gravity with center value on TextView
<TextView
android:text="welcome text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
/>
android:layout_gravity="center"
or
android:gravity="center"
both works for me.
Or check this out this will help align all the elements at once.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/showdescriptioncontenttitle"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_gravity="center"
android:gravity="center_horizontal"
>
<TextView
android:id="#+id/showdescriptiontitle"
android:text="Title"
android:textSize="35dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
If you want to change the alignment from the mainActivity.java file, then use the gravity. For example:
chatMsg.setGravity(Gravity.RIGHT);
chatMsg is my textView. I wanted the text to be right aligned and this code worked well.
you can easily done this by adding these lines
android:layout_width="match_parent"
android:gravity="center"
android:textAlignment="center"

How to add a toolbar to an Android Activity?

The official Twitter client for Android has a nice toolbar when you click on a message that lets you retweet, reply, etc. How can I re-create a toolbar like that?
For the LinearLayout approach, the code is simple, just add some weights and play around with what your looking for. This gives you a rough idea of the approach:
<EditText android:text="Example Layout"
android:layout_width="match_parent"
android:id="#+id/editText"
android:layout_height="wrap_content"
android:layout_weight="0.95"></EditText>
<LinearLayout
android:layout_width="fill_parent"
android:orientation="horizontal"
android:weightSum="1.0"
android:padding="1px"
android:layout_height="wrap_content"
android:layout_weight="0.05">
<ImageButton android:id="#+id/testButton1"
android:layout_weight="0.2"
android:background="#drawable/btn"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<ImageButton android:id="#+id/testButton2"
android:layout_weight="0.2"
android:background="#drawable/btn"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<ImageButton android:id="#+id/testButton3"
android:layout_weight="0.2"
android:background="#drawable/btn"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<ImageButton android:id="#+id/testButton4"
android:layout_weight="0.2"
android:background="#drawable/btn"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<ImageButton android:id="#+id/testButton5"
android:layout_weight="0.2"
android:background="#drawable/btn"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
And results in something like this: http://i.stack.imgur.com/bShgb.png
You may also be happy with ActionBar as well, just watch the API requirements: http://developer.android.com/guide/topics/ui/actionbar.html
We call it QuickAction:
Find more about how to implement it here: http://www.londatiga.net/it/how-to-create-quickaction-dialog-in-android/

Categories