Weird issue with MaterialCardView - java

I'm trying to make a rounded shape like this :
The code below works great on a device with 6.7" as screen size.
<com.google.android.material.card.MaterialCardView
android:id="#+id/resultHolder"
android:layout_width="232dp"
android:layout_height="376dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:gravity="center"
android:rotation="15"
android:outlineProvider="background"
android:theme="#style/Theme.MaterialComponents.Light"
app:cardBackgroundColor="#color/black"
app:cardCornerRadius="120dp"
app:cardElevation="0dp"
app:strokeColor="#color/green_neon"
app:strokeWidth="6dp">
<ImageView
android:src="#drawable/thumbnail"
android:id="#+id/resultImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="centerCrop" />
</com.google.android.material.card.MaterialCardView>
But when I test the app on device with 5.1" as screen size. I get this :

You're setting
android:layout_width="232dp"
android:layout_height="376dp"
which are fixed values when you put app:cardCornerRadius="120dp" which is very high it does not get the space to complete it's 120dp radius on a smaller screen..
Use a background drawable or a different solution. This solution will work perfectly only in the 6.7 inch screen that you have and only that

Related

Java Android Studio: Image View not loading from Drawable folder

I've been working on a simple java application, and wanted to add a logo as the homescreen is fairly empty. I tried using an image view, and after it didn't work I googled tutorials to make sure I was initiating it correctly. I didn't see any difference, but the actual image wouldn't load.
Xml code for image view:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src = "#drawable/logo"
android:layout_gravity="center"
android:contentDescription="#string/logo"
android:layout_below="#+id/textView"
android:layout_above="#+id/txtBody"
android:layout_alignRight="#+id/txtBody"
android:layout_alignEnd="#+id/txtBody"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:visibility="visible"
android:maxHeight="600dp"
android:maxWidth="600dp"
android:minHeight="100dp"
android:minWidth="100dp"
android:clickable="false" />
And here's a picture of the layout preview (With an arrow pointing to the outline of where it should be, and the resource circled in the correct folder) along with a picture of the logo
Any dice if you add
android:scaleType="fitCenter"
android:adjustViewBounds="true"
Edit: also are we sure nothing is overlapping it?

how to define correctly textview width

Hi everyone i got a little problem about the width of my textview
it looks like this
as you can see, my textview is bigger than my text :/
here is my code :
<?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="horizontal"
android:weightSum="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="destinataire"
android:id="#+id/tv_destinataire"
android:layout_gravity="left|top"
android:layout_weight="1"
android:layout_marginLeft="25dp"
android:layout_marginTop="10dp"
android:padding="10dp"
android:layout_marginBottom="10dp"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="envoyeur"
android:id="#+id/tv_envoyeur"
android:layout_gravity="right|top"
android:layout_weight="1"
android:layout_marginRight="25dp"
android:layout_marginTop="10dp"
android:padding="10dp"
android:textColor="#ffffff"
android:layout_marginBottom="10dp" />
</LinearLayout>
i would like something like this, if the message contain some word well "wrap_content" whereas is long long message something like maximum 70% of width :
thank you :)
You can reach what you said adding some logic directly on your activity and not in the XML file. You can set the width by Java code with something like:
TextView t = (TextView)findViewById(R.id.myTextView);
where myTextView is the id that you declared in your XML.
Now go ahead and write some logic...
If is necessary:
t.setWidth(200);
Note that 200 is only an example, you can calculate the width you need before.
If I were you, I might use the relative layout and add maxWidth limit to TextView containing text, and as the dialog going, just place the TextView below the last TextView and use alignStart/alightParentStart and alignEnd/alignParentEnd to indicate who's speaking.
when a new message arrives
prepare your relative layout parameter, add layout rules
set text, maxwidth limit, layout parameter and other style you want for your text
add the view to a scrollable relative layout container
Sorry for my bad English in case of you have any reading problem. :)

Stretching an imageview, different on each screen

I have imageview with resolution of 430x871 in folders xxhdpi, xhdpi and hdpi. But when I run app, on 5" Xperia Z (xxhdpi) screen it works normally (1. picture) but on 4.7" Amazon Phone (xhdpi) it's smaller (2. picture). Altough when I run it on Amazon Fire HD 8.9 or 7 or 6, (all xdpi) it works too...
How can I stretch in on that Amazon Phone?
Here is XML code of the Imageview:
<ImageView
android:id="#+id/matter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:gravity="center"
android:src="#drawable/matter"
/>
if aspect ratio is not important for you use:
android:scaleType="fitXY"
but if yes try this:
android:scaleType="fitCenter"
and also change this:
android:layout_height="match_parent"
There's a property called :
android:scaleType="fitXY"
inside ImageView tag.

Creating login layout like in facebook app for android

I want to make an app with a login activity/layout similar to what Facebook app has. What I mean is when text field is focused soft keyboard pushes the entire view up but not squashing a logo. I have tried android:windowSoftInputMode="adjustPan/adjustResize" but it is not what I was trying to achieve.
I found this question on SO perhaps it will make things clearer, but it has no solution to the problem.
I have also tried various layouts types but it soft keyboard only pushes the focused < EditText > up. Please guide me.
UPDATE:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#DDDDDD">
<RelativeLayout
android:height="0dp"
android:layout_weight="1"
android:layout_height="0dp"
android:layout_width="fill_parent"
android:background="#ff0000">
<ImageView
android:layout_centerVertical="true"
android:layout_height="fill_parent"
android:layout_width="fill_parent"></ImageView>
</RelativeLayout>
<RelativeLayout
android:height="0dp"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="fill_parent"
android:background="#00ff00">
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#0000ff"
android:height="0dp" >
<Button
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Log in"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="4dp"
android:hint="password"
android:inputType="textPassword" >
</EditText>
<EditText
android:id="#+id/editText1"
android:hint="login"
android:padding="4dp"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" ></EditText>
</RelativeLayout>
</LinearLayout>
UPDATE working solution
I can't paste here the entire xml file, but the structure should be enough.
Based on Gabe Sechan's answer.
Layout{
Layout top weight 1
Layout mid weight 1
Layout bot weight 1
}
Child layouts have set to:
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" // should be changed accordingly to your layout design.
And here is a Java code for the activity(keyboard up/down):
View top, mid, bot;
final View activityRootView = findViewById(R.id.loginLayout);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView()
.getHeight() - activityRootView.getHeight();
if (heightDiff > 100) { //keyboard up
mid.setVisibility(View.INVISIBLE);
top.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, 0, 0f));
bot.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, 0, 1f));
} else {// keyboard down
// v.setVisibility(View.VISIBLE);
mid.setVisibility(View.VISIBLE);
top.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, 0, 2f));
bot.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, 0, 3f));
}
}
});
On keyboard up you need to change weights accourding to keyboard up design and on keyboard down change back to the default(layout that you've set via xml/java). I've tested the code on 2.3.x and up.
And don't forget to use android:inputType="textFilter" for the login&password EditText's to remove suggestions on input and save some pixels. In your manifest for the activity android:windowSoftInputMode="adjustResize|stateHidden". stateHidden is used so that keyboard won't be up when activity loads. Hope it helps. Good luck.
They're doing it with relative layouts, adjustResize, and android:layout_centerVertical. Basically, they have a linear layout for their main layout, with 3 equally weighted relative layouts inside of it. Each is set to 0dp height, so they take up equal thirds of the screen. The top RelativeLayout holds the logo, centered vertically. The middle holds the login fields and button, centered vertically one on top of the other. The bottom one holds the copyright text, aligned to bottom. The end result is that when the keyboard comes up, the 3 relative layouts get resized to take 1/3 of the new screen. Then their elements are centered in the new screen.
Remember you need the adjustResize window mode to get this, if you use pan it will just move up and the logo will scroll off center.
In Eclipse, go to File|New|Other and in the Wizard that follows, select Android Activity, then on the next page, select LoginActivity from the list of activities. This has the exact layout you're talking about, and you can use that as a framework. It uses a ScrollView to achieve the effect you're looking for.

Android - Sliding Drawer Hang Out (Stuck Out)?

how to make the content of the sliding drawer a little "stuck out" (hang out)?
After pull
You can't do it with the stock widget.
However, someone asked about this before and an answer was posted by someone who has made a class to allow you to do just that. Follow the answer link for instructions.
The code is here
Here is an example to do it within your XML file
<SlidingDrawer
android:id="#+id/drawer"
android:layout_width = "320dp" //set what ever dimensions you find appopriate
android:layout_height = "440dp"
android:orientation = "vertical"
android:handle="#+id/handle"
android:content="#+id/content">
<ImageView
android:id="#+id/handle"
android:layout_width="48dp" // Set your dimensions. This will be your handle
android:layout_height="48dp"
android:src="#drawable/putHandleImageHere" />
<TextView android:id="#+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text ="Hello World" />
---------------EDIT------------------
I reread your question and I see that your trying to have the content hang out. Try using
android:paddingTop="10dp"
or
android:layout_marginTop="10dp"
Let me know if that worked, good luck.

Categories