I'm trying to make a simple listView with string objects only inside dialog; the problem is the list item is not width match_parent as i set:
the list view is the root element:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ff0000"
android:id="#+id/lvLinks"
android:layout_height="match_parent"/ >
list item:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="7dp"
android:textSize="15sp"
android:background="#0000ff"
android:layout_height="match_parent" />
adapter:
ListView lv = (ListView) findViewById(R.id.lvLinks);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,R.layout.list_item_links,items);
lv.setAdapter(adapter);
I've tested it on many OS, this is not the problem
FIXED:
answer
<?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" >
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ff0000"
android:id="#+id/lvLinks"
android:layout_height="match_parent" />
</LinearLayout>
Why don't you put your ListView inside LinearLayout or RelativeLayout having width match_parent or fill_parent dialog.xml
This may be not exact solution but you can try below xml for your list item
<?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="wrap_content"
android:orientation="horizontal"
<TextView
android:id="#android:id/text1"
android:layout_width="match_parent"
android:padding="7dp"
android:textSize="15sp"
android:background="#0000ff"
android:layout_height="wrap_content" />
</LinearLayout>
The proposed answer didn't solve anything for me. The issue actually only showed on MIUI devices, not on Google's stock nor Samsung or OnePlus.
Instead I added the following code to my custom listView constructor:
addOnLayoutChangeListener(this);
And the listener:
#Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom)
{
int I = getChildCount();
for(int i = 0; i < I; i++)
{
getChildAt(i).requestLayout();
}
}
Related
I've a simply layout like following for a custom view:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="#+id/someInnerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/iv_arrow"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerHorizontal="true"
android:src="#drawable/arrow"/>
</RelativeLayout>
And this function in my custom view:
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mForceSquareLayout) {
int w = getMeasuredWidth();
int h = getMeasuredHeight();
if (w != h) {
int size = Math.min(w, h);
setMeasuredDimension(size, size);
}
}
}
Any my layout looks like following:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
style="?toolbar_view_theme"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?colorPrimary"
android:elevation="16dp" />
<CustomView
android:id="#+id/wheel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="16dp"
app:square_layout="true"
app:arrow_image="#drawable/ic_arrow_drop_down_white_24dp"
app:background_color="?colorPrimary" />
</LinearLayout>
</layout>
The problem is, that if the square logic does resize the view (which it does in landscape mode), my custom views child iv_arrow is not shown anymore. It looks like my square logic does not relayout the children. Any ideas how to solve this?
I tried requestLayout on the view and on the child with no success...
I want to move my layout up when the keyboards shows on my app . I used adjustResize , adjustPan and both of them for the activity but non of them works and it still shows the keyboard on the form and does not move the layout above .
this is my layout :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
............... my views
</LinearLayout>
</ScrollView>
</RelativeLayout>
how can I make it works ?
try like this
final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
if (heightDiff > dpToPx(getApplicationContext(), 200)) { // if more than 200 dp, it's probably a keyboard...
// ... do something here
}
}
});
public static float dpToPx(Context context, float valueInDp) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, metrics);
}
andjust add this in your xml
android:id="#+id/activityRoot"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
**android:id="#+id/activityRoot"**>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
............... my views
</LinearLayout>
</ScrollView>
</RelativeLayout>
I would like to implement a screen where I have a Card view containing a RecyclerView.
The CardView should of the same height of the content of the recycler view, this means that if the RecyclerView has few item, I should see the bottom corners and the bottom shadow of the card but if the RecyclerView has many items, the Card view should "scroll" with the RecyclerView to have the bottom corners and shadow of the cardview at the bottom of the RecylerView.
Here what it should look like when the RecyclerView is at top :
When the user begins to scroll, the top corners disappear with the RecyclerView scrolling :
And finally, when the user reaches the bottom of the RecyclerView, the bottom corners and the shadow of the CardView appears :
From now, I managed to have a working implementation by putting the RecyclerView inside the CardView and the CardView inside a NestedScrollView but this breaks the fling gesture...
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:clipChildren="false"
android:id="#+id/containerLayout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
tools:ignore="MissingPrefix">
<android.support.v4.widget.NestedScrollView
android:clipToPadding="false"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="85dp"
android:paddingRight="85dp"
android:paddingTop="16dp">
<android.support.v7.widget.CardView
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:cardBackgroundColor="?android:attr/windowBackground">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</android.support.v7.widget.CardView>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
Do you have any hints or idea on how I could implement such design ? I guess that CoordinatorLayout could help me but I couldn't find anything ...
Thank you
Picking up Oknesif's idea of a manipulated adapter, I made an adapter with three layouts (topitem, middleitem, bottomitem) with two XML drawable shapes for topitem and bottomitem. Thus, I was able to completely get rid of the NestedScrollView and the CardView.
This is what it looks like:
And here is the code. First, MainActivity:
public class MainActivity extends AppCompatActivity {
final static int LIST_SIZE = 100;
final static int TOP = 0;
final static int BOTTOM = LIST_SIZE;
final static int MIDDLE = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
final ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < LIST_SIZE; i++) {
list.add(i);
}
class Viewholder extends RecyclerView.ViewHolder {
TextView textView;
Viewholder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView);
}
}
RecyclerView recyclerView = findViewById(R.id.recyclerView);
final RecyclerView.Adapter<Viewholder> adapter = new RecyclerView.Adapter<Viewholder>() {
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
#Override
public Viewholder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case TOP:
return new Viewholder(inflater.inflate(R.layout.topitem, parent, false));
case BOTTOM:
return new Viewholder(inflater.inflate(R.layout.bottomitem, parent, false));
case MIDDLE:
default:
return new Viewholder(inflater.inflate(R.layout.middleitem, parent, false));
}
}
#Override
public void onBindViewHolder(Viewholder holder, int position) {
holder.textView.setText(String.valueOf(list.get(position)));
if (position != 0 && position != LIST_SIZE - 1) {
int color = position % 2 == 0 ? android.R.color.holo_orange_dark : android.R.color.holo_orange_light;
holder.itemView.setBackgroundColor(getResources().getColor(color));
}
}
#Override
public int getItemCount() {
return LIST_SIZE;
}
#Override
public int getItemViewType(int position) {
int itemViewType;
switch (position) {
case 0:
itemViewType = TOP;
break;
case LIST_SIZE - 1:
itemViewType = BOTTOM;
break;
default:
itemViewType = MIDDLE;
}
return itemViewType;
}
};
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
}
}
res/layout/activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/containerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingLeft="25dp"
android:paddingRight="25dp" />
</android.support.design.widget.CoordinatorLayout>
res/layout/topitem.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/topbackground"
android:layout_marginTop="50dp"
android:textAlignment="center"
android:textColor="#android:color/white"
android:textSize="24sp"
android:textStyle="bold" />
res/layout/middleitem.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="#android:color/white"
android:textSize="24sp"
android:textStyle="bold" />
res/layout/bottomitem.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bottombackground"
android:layout_marginBottom="50dp"
android:textAlignment="center"
android:textColor="#android:color/white"
android:textSize="24sp"
android:textStyle="bold" />
res/drawable/topbackground.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
<solid android:color="#android:color/holo_orange_dark" />
</shape>
res/drawable/bottombackground.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp" />
<solid android:color="#android:color/holo_orange_light" />
</shape>
EDIT:
Adding this line to the bottom XML item layouts:
android:elevation="12dp"
and changing the background to white, gives the following result:
it's just a simple line of code
recycler.setNestedScrollingEnabled(false);
and don't forget to make cardview height to wrap_content
I have a suggestion based on the Constraintlayout which I have used before.
You can create two Guideline to set the starting and ending position of the CardView during the scrolling process. Let me illustrate the XML for the start position of the view
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:clipChildren="false"
android:id="#+id/containerLayout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
tools:ignore="MissingPrefix">
<android.support.constraint.Guideline
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/guideline"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.1"/>
<android.support.constraint.Guideline
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/guideline2"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.9"/>
<android.support.v7.widget.CardView
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:cardBackgroundColor="?android:attr/windowBackground"
app:layout_constraintTop_toTopOf="#+id/guideline">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
</android.support.v7.widget.CardView>
here I am assuming that you want to leave roughly 10% of screen space empty on top. If you want less or more, please adjust.
Once the user starts scrolling you can adjust the top constraint of the Cardview to the top of the parent and once he reaches the bottom of the list you can adjust the bottom constraint of the Cardview to the guideline2 which will leave 10% of screen space below.
This should achieve the desired effect without much performance issues since you are doing away with the Scrollview.
Please let me know if you need me to elaborate any part of my answer in more detail.
I am trying to have the content of the ListView centered when displayed in a dialog.
This is my code to show the ListView
String[] myItems = {"item1", "item2", "item3"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, R.layout.setting_layout, myItems);
ListView list = (ListView) deleteDialog.findViewById(R.id.listView1);
list.setAdapter(adapter);
list.setFadingEdgeLength(5);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> paret, View viewClicked, int position, long id) {
// TODO Auto-generated method stub
}
}
And this is the xml part:
TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_gravity="center"
android:textSize="25sp"
and this is the layout of delete dialog:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal" >
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:dividerHeight="5dp"
android:gravity="center">
</ListView>
Its running but the content is always displayed to left.
I tried this: How to set the text at center in ListView android application?
but it didn't work.
Hope anyone can guide me through.
this will work for u
<?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="wrap_content" >
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:dividerHeight="5dp"
android:gravity="center">
</ListView>
You can try with
android:layout_gravity="center_vertical|center_horizontal"
I hope it helps
Change your LinearLayout to:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal">
Hi i have an activity class that has a layout, the layout itself has a custom view, this is the activity class:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class Minesweeper extends Activity {
public static final String KEY_DIFFICULTY = "minesweeper.difficulty";
public static final int DIFFICULTY_EASY = 1;
public static final double DIFFICULTY_MEDIUM = 1.5;
public static final int DIFFICULTY_HARD = 2;
private int diff;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
diff = getIntent().getIntExtra(KEY_DIFFICULTY, DIFFICULTY_EASY);
diff++;
Log.d("diff", String.valueOf(diff));
Mine.setlevel(diff);
setContentView(R.layout.minesweeper);
Mine.clearMines();
}
}
and here's the layout of the activity:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background" >
<com.mine_sweeper.Table
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/table"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>`
everything's good on graphical view in eclipse on layout file but when i try to add a button to the layout file it only sticks to the left side of my custom view witch is a 9*9 grid, so my question is how to move it under my custom veiw?
i have tried changing custom view's layout_width and layout_height already.
Try this. User vertical or horizontal to change positions in nested LinearLayout. android:orientation="vertical" or android:orientation="horizontal"
`
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Button" />
<com.mine_sweeper.Table
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/table"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
`
You can also user other layouts like GridLayout etc. this is just one approach using LinearLayout