I am not able to get width and height of RelativeLayout, how can I get this? My layout.xml file is
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/layoutMain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="50dp" >
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
and I just get programtically height and width of RelativeLayout using its built-in provided method like,
public class CustomShowcaseActivity extends Activity { static float width;
static float height; RelativeLayout relMain;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_showcase);
relMain= (RelativeLayout) findViewById(R.id.layoutMain);
width = relMain.getMeasuredWidth();
height = relMain.getMeasuredHeight();
width = relMain.getWidth();
height = relMain.getHeight();
System.out.println("=== Width : " + width);
System.out.println("=== height : " + height);
} }
Can anyone help me with this issue?
override this method first.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
getRelativeLayoutInfo();
}
Try by using only getHeight() and getWidth()
private void getRelativeLayoutInfo(){
relMain = (RelativeLayout) findViewById(R.id.layoutMain);
w = relMain.getWidth();
h = relMain.getHeight();
Log.v("W-H", w+"-"+h);
}
Related
Here a beginner at Android studio.
I'm creating a simple agenda app, where you can add, edit, delete and search contacts.
I used a recycle view but after adding the search option, the recycle view just shows me one contact.
The contacts are being added as appear when I search for them.
Any idea on how to fix it?
It tried changing the layout but nothing works.
My main activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<SearchView
android:id="#+id/txtBuscar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listacontactos"
android:layout_width="match_parent"
android:layout_height="591dp" />
</LinearLayout>
</LinearLayout>
Here is the Adapter: https://github.com/soymartanegro/agendaApp/blob/main/src/main/java/com/example/agenda/adaptadores/ListaContactosAdapter.java
Main Java: https://github.com/soymartanegro/agendaApp/blob/main/src/main/java/com/example/agenda/MainActivity.java
Thanks a lot.
I have used the below code it's working perfectly. Please check it. Currently, I'm using dummy data for testing purposes.
activity_main.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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<SearchView
android:id="#+id/txtBuscar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listacontactos"
android:layout_width="match_parent"
android:layout_height="591dp" />
</LinearLayout>
</LinearLayout>
lista_item_contacto.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/viewNombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
contactos
public class contactos {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
ListaContactosAdapter.java
public class ListaContactosAdapter extends RecyclerView.Adapter<ListaContactosAdapter.ContactoViewHolder> {
ArrayList<contactos> listaContactos;
ArrayList<contactos> listaOriginal;
public ListaContactosAdapter(ArrayList<contactos> listaContactos){
this.listaContactos = listaContactos;
listaOriginal = new ArrayList<>();
listaOriginal.addAll(listaContactos);
}
#NonNull
#Override
public ContactoViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.lista_item_contacto, null , false);
return new ContactoViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ContactoViewHolder holder, int position) {
holder.viewName.setText(listaContactos.get(position).getName());
}
public void filtrado(String txtBuscar){
int longitud = txtBuscar.length();
if (longitud == 0){
listaContactos.clear();
listaContactos.addAll(listaOriginal);
} else {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
List<contactos> collecion = listaContactos.stream().filter(i->i.getName().toLowerCase().contains(txtBuscar.toLowerCase()))
.collect(Collectors.toList());
listaContactos.clear();
listaContactos.addAll(collecion);
} else {
for(contactos c: listaOriginal){
if (c.getName().toLowerCase().contains(txtBuscar.toLowerCase())){
listaContactos.add(c);
}
}
}
}
notifyDataSetChanged();
}
#Override
public int getItemCount() {
return listaContactos.size();
}
public class ContactoViewHolder extends RecyclerView.ViewHolder {
TextView viewName, viewTelephone, viewEmail;
public ContactoViewHolder(#NonNull View itemView) {
super(itemView);
viewName = itemView.findViewById(R.id.viewNombre);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Context context = view.getContext();
}
});
}
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener{
SearchView txtBuscar;
RecyclerView listaContactos;
ArrayList<contactos> listaArrayContactos;
ListaContactosAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtBuscar = findViewById(R.id.txtBuscar);
listaContactos = findViewById(R.id.listacontactos);
listaContactos.setLayoutManager(new LinearLayoutManager(this));
listaArrayContactos = new ArrayList<>();
contactos c1 = new contactos();
c1.setName("Hello");
contactos c2 = new contactos();
c2.setName("Android");
contactos c3 = new contactos();
c3.setName("Studio");
listaArrayContactos.add(c1);
listaArrayContactos.add(c2);
listaArrayContactos.add(c3);
adapter = new ListaContactosAdapter(listaArrayContactos);
listaContactos.setAdapter(adapter);
//Revisar si no funciona y cambiar de lugar
txtBuscar.setOnQueryTextListener(this);
}
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
#Override
public boolean onQueryTextChange(String s) {
adapter.filtrado(s);
return false;
}
}
I need to add inflate layout programmatically in LinearLayout, but widht not divide equally in layout.here i attached output screen. Please help me to solve.
following is my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:id="#+id/hsv_category_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:scrollbars="none">
<LinearLayout
android:id="#+id/ll_placeHolder"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
and following are my class:
public class ScrollTest extends Activity {
HorizontalScrollView hsv_category_list;
LinearLayout ll_placeHolder;
View layoutView[];
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scroll_test);
hsv_category_list = (HorizontalScrollView) findViewById(R.id.hsv_category_list);
ll_placeHolder = (LinearLayout) findViewById(R.id.ll_placeHolder);
layoutView = new View[2];
for (int i = 0; i < 2; i++) {
layoutView[i] = LayoutInflater.from(this).inflate(R.layout.test_scroll_itwm, null);
layoutView[i].setId(i);
ll_placeHolder.addView(layoutView[i], new LinearLayout.LayoutParams(
100,
200));
}
}
}
I have a possible overlapping fragment containing listView. The layout looks fine in the preview window, however, when I run it on device or Emulator the appbar goes missing.
Main XML
<?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="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".GuiseMainActivity"
android:orientation="vertical">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:id="#+id/fragmentProps"></fragment>
</RelativeLayout>
Fragment XML
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:gravity="top|center"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/propCategoryListView"></ListView>
</LinearLayout>
Main Class
public class GuiseMainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentProps fragmentProps = new FragmentProps();
fragmentTransaction.replace(android.R.id.content,fragmentProps);
fragmentTransaction.commit();
}}
Fragment Class
public class FragmentProps extends Fragment{
private View PropsFragmentView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
PropsFragmentView = inflater.inflate(R.layout.fragment_props, container, false);
String[] propsCategories ={"Recently used", "Popular", "New", "Personalities", "Moustaches / Beard", "Caps/ Hats", "Glasses", "Banners"};
ListAdapter propsCategoryListAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, propsCategories);
ListView propCategoryListView = (ListView) PropsFragmentView.findViewById(R.id.propCategoryListView);
propCategoryListView.setAdapter(propsCategoryListAdapter);
propCategoryListView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String categoryPicked = "You Picked: " + String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(getActivity(), categoryPicked, Toast.LENGTH_LONG).show();
}
});
return PropsFragmentView;
}}
Result
I found the solution, MainActivity Extends AppCompatActivity then add these lines in onCreate method:
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.applogo);
getSupportActionBar().setTitle("");
Thanks guys.
I find this very strange behavior of listview and it acts differently on Android 2.1 and 4.1
Here are the 2 classes I wrote which google advises to use for faster performance of ListView.
How to reproduce: Add an entry, check the first checkbox, then add more entries than one page. The checked box floats through the elements!
Here is a small project I wrote: http://tinyurl.com/strange-listview
Anyone can explain the issue or a workaround?
public class AlarmManagerActivity extends Activity {
private PerformanceArrayAdapter mPerformaceArrayAdapter;
private ListView mListView;
private ArrayList<String> mListItems = new ArrayList<String>();
private int mCounter = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListView = (ListView) findViewById(R.id.listViewCondition);
mPerformaceArrayAdapter = new PerformanceArrayAdapter(this, mListItems);
mListView.setAdapter(mPerformaceArrayAdapter);
}
public void onClickAdd(View view){
mListItems.add("" + mCounter++);
mPerformaceArrayAdapter.notifyDataSetChanged();
}
}
And here is the main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="#+id/buttonAdd" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Add New Alarm"
android:onClick="onClickAdd" />
<ListView android:id="#+id/listViewCondition"
android:layout_height="wrap_content" android:layout_width="match_parent"></ListView>
</LinearLayout>
And the performance adapter class.
public class PerformanceArrayAdapter extends ArrayAdapter<String> {
private final Activity context;
private final ArrayList<String> mListItems;
static class ViewHolder {
public TextView textView1;
public TextView textView2;
public CheckBox checkBox;
}
public PerformanceArrayAdapter(Activity context, ArrayList<String> listItems) {
super(context, R.layout.row_layout, listItems);
this.context = context;
this.mListItems = listItems;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.row_layout, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.textView1 = (TextView) rowView.findViewById(R.id.textView1);
viewHolder.textView2 = (TextView) rowView.findViewById(R.id.textView2);
viewHolder.checkBox = (CheckBox) rowView.findViewById(R.id.checkBox1);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
holder.textView1.setText(mListItems.get(position));
return rowView;
}
}
Here is the row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView android:id="#+id/textView1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" android:text="TextView1"></TextView>
<TextView android:id="#+id/textView2" android:layout_below="#+id/textView1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="TextView2"></TextView>
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/checkBox1"
android:layout_alignParentRight="true" android:focusable="false"
android:focusableInTouchMode="false"></CheckBox>
</RelativeLayout>
I found the solution, thanks to Vogella :)
I had to use the Domain Model mentioned here to track both the text view and check box.
http://www.vogella.com/articles/AndroidListView/article.html#listadvanced_interactive
I'm trying to implement a ListView with a title. However, with my code below, the following happens :
the title appears over the first list element.
There is a EditText which appears in front of the others on the top.
My android Activity
public class LoanRepaymentActivity extends ListActivity implements View.OnClickListener {
private Location lastKnownLocation;
private ArrayList<CurrentRepaymentInstallment> loansForRepayment;
#Override
public void onCreate(Bundle savedInstanceState){
lastKnownLocation = Utility.getLastKnownLocation(this);
super.onCreate(savedInstanceState);
Intent intent = getIntent();
RepaymentInfo repaymentInfo = (RepaymentInfo)intent.getSerializableExtra(Constants.CURRENT_REPAYMENT_INSTALLMENT);
loansForRepayment = repaymentInfo.getLoansForRepayment();
boolean repaymentsNotPresent = loansForRepayment == null || loansForRepayment.isEmpty();
if(repaymentsNotPresent){
Dialog dialog = Utility.getDialogWithText(LoanRepaymentActivity.this, getText(R.string.noLoansForRepayment).toString());
Utility.getDialogButton(dialog, LoanRepaymentActivity.this, PartnerGroupListActivity.class, intent.getStringExtra(Constants.ACECSS_TOKEN_PARAM));
dialog.show();
}
setContentView(R.layout.loan_repayments);
ArrayAdapter<CurrentRepaymentInstallment> adapter = new LoanRepaymentListAdapter(this, loansForRepayment);
View footer = getLayoutInflater().inflate(R.layout.loan_disbursement_footer, null);
getListView().addFooterView(footer);
setListAdapter(adapter);
if(!repaymentsNotPresent) {
TextView textView = (TextView)findViewById(R.id.screenTitle);
String currentInstallmentLabel = getText(R.string.currentInstallmentLabel).toString() + repaymentInfo.getCurrentGroupInstallment();
textView.setText(currentInstallmentLabel);
}
Button button = (Button)findViewById(R.id.disburse);
button.setOnClickListener(this);
}
The XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView android:id="#+id/screenTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TextView>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+layout/loan_disbursement_footer"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/borrowerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="14sp"
>
</TextView>
<TextView
android:id="#+id/loanAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="14sp"
>
</TextView>
<TextView
android:id="#+id/installmentNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="12sp"
>
</TextView>
<TextView
android:id="#+id/estimatedTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="12sp"
>
</TextView>
</LinearLayout>
<EditText
android:id="#+id/repaymentAmount"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:inputType="numberDecimal"
android:textSize="12sp"
>
</EditText>
Custom Array Adapter
public class LoanRepaymentListAdapter extends ArrayAdapter<CurrentRepaymentInstallment> {
private final List<CurrentRepaymentInstallment> loansForRepayment;
private final Activity context;
public LoanRepaymentListAdapter(Activity context, List<CurrentRepaymentInstallment> loansForRepayment) {
super(context, R.layout.loan_repayments, loansForRepayment);
this.context = context;
this.loansForRepayment = loansForRepayment;
}
static class ViewHolder {
protected TextView borrowerName;
protected TextView loanAmount;
protected TextView installmentNumber;
protected TextView estimatedTotal;
protected EditText repaymentAmount;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.loan_repayments, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.borrowerName = (TextView) view.findViewById(R.id.borrowerName);
viewHolder.loanAmount = (TextView) view.findViewById(R.id.loanAmount);
viewHolder.installmentNumber = (TextView) view.findViewById(R.id.installmentNumber);
viewHolder.estimatedTotal = (TextView) view.findViewById(R.id.estimatedTotal);
viewHolder.repaymentAmount = (EditText) view.findViewById(R.id.repaymentAmount);
// viewHolder.repaymentAmount.setEditableFactory(Editable.Factory.getInstance());
viewHolder.repaymentAmount.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable arg0) {
}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String repaymentAmount = s.toString();
try {
loansForRepayment.get(position).setRepaymentAmount(Float.parseFloat(repaymentAmount));
} catch (Exception ex){
loansForRepayment.get(position).setRepaymentAmount(0.0F);
}
}
});
view.setTag(viewHolder);
viewHolder.repaymentAmount.setTag(loansForRepayment.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).repaymentAmount.setTag(loansForRepayment.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.borrowerName.setText(loansForRepayment.get(position).getLoanProfileBasicInfo().getBorrowerBasicInfo().getFirstName());
holder.loanAmount.setText("Rs. " + Float.toString(loansForRepayment.get(position).getLoanProfileBasicInfo().getLoanAmountInPaisa()/100));
holder.estimatedTotal.setText("Rs. " + Float.toString(loansForRepayment.get(position).getEstimatedTotalAmount()/100));
holder.installmentNumber.setText("Inst no : " + Integer.toString(loansForRepayment.get(position).getInstallmentNumber()));
float repaymentAmt = loansForRepayment.get(position).getRepaymentAmount();
if(repaymentAmt != 0.0) holder.repaymentAmount.setText(Float.toString(repaymentAmt));
return view;
}
}
Would be great if someone could tell me how to do this.
Set a new layout for the title and add it to the ListView as a header.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
>
<TextView android:id="#+id/repaymentScreenTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/repaymentsTitle" >
</TextView>
</LinearLayout>
Replace this block with the one below it in your Activity
if(!repaymentsNotPresent) {
TextView textView = (TextView)findViewById(R.id.screenTitle);
String currentInstallmentLabel = getText(R.string.currentInstallmentLabel).toString() + repaymentInfo.getCurrentGroupInstallment();
textView.setText(currentInstallmentLabel);
}
View headerView = getLayoutInflater().inflate(R.layout.loan_repayment_title, null);
TextView titleView = (TextView) headerView.findViewById(R.id.repaymentScreenTitle);
String titlePrefix = (String) getText(R.string.groupInstallmentLabel);
titleView.setText(titlePrefix + " " + repaymentInfo.getCurrentGroupInstallment());
getListView().addHeaderView(headerView);