Android Grid Menu Layout - android-layout

i am a beginner in android programming.( i am using android studio for coding)
I am trying to design a dashboard for my android application taking guidance from the following link
and its working well, but i want to make it as per
Like This Image
i need 2 column layout with icon image, Title 1 and title 2 with a Background image.
can anyone help me.
thank you

you have to use customGridView using BaseAdapter. In customGridView show the ImageView with TextView for each list item.
main.xml
<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"
tools:context=".MainActivity" >
<GridView
android:id="#+id/gridViewCustom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:columnWidth="80dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
</RelativeLayout>
grid_row.xml
<LinearLayout 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"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:src="#drawable/ic_launcher" >
</ImageView>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="15sp" >
</TextView>
</LinearLayout>
CustomGridViewMainActivity.java
public class CustomGridViewMainActivity extends Activity
{
GridView gridView;
GridViewCustomAdapter grisViewCustomeAdapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView=(GridView)findViewById(R.id.gridViewCustom);
// Create the Custom Adapter Object
grisViewCustomeAdapter = new GridViewCustomAdapter(this);
// Set the Adapter to GridView
gridView.setAdapter(grisViewCustomeAdapter);
// Handling touch/click Event on GridView Item
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
String selectedItem;
if(position%2==0)
selectedItem="Facebook";
else
selectedItem="Twitter";
Toast.makeText(getApplicationContext(),"Selected Item: "+selectedItem, Toast.LENGTH_SHORT).show();
}
});
}
}
then set the adapter with your customized view
GridViewCustomAdapter.java
public class GridViewCustomAdapter extends ArrayAdapter
{
Context context;
public GridViewCustomAdapter(Context context)
{
super(context, 0);
this.context=context;
}
public int getCount()
{
return 24;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.grid_row, parent, false);
TextView textViewTitle = (TextView) row.findViewById(R.id.textView);
ImageView imageViewIte = (ImageView) row.findViewById(R.id.imageView);
if(position%2==0)
{
textViewTitle.setText("Facebook");
imageViewIte.setImageResource(R.drawable.facebook);
}
else
{
textViewTitle.setText("Twitter");
imageViewIte.setImageResource(R.drawable.twitter);
}
}
return row;
}
}
Output:

At last, I found the solution...
your XML File`
<?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"
android:background="#drawable/bg"
android:padding="16dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ededed"
android:drawableLeft="#drawable/ic1"
android:paddingBottom="5dp"
android:paddingLeft="16dp"
android:paddingRight="40dp"
android:paddingTop="8dp"
android:text="\nButton"
android:layout_marginBottom="5dp"
android:textColor="#000000" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#ededed"
android:drawableLeft="#drawable/ic1"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="40dp"
android:paddingTop="8dp"
android:text="\nButton"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ededed"
android:drawableLeft="#drawable/ic1"
android:paddingBottom="5dp"
android:paddingLeft="16dp"
android:paddingRight="40dp"
android:paddingTop="8dp"
android:text="\nButton"
android:layout_marginBottom="5dp"
android:textColor="#000000" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#ededed"
android:drawableLeft="#drawable/ic1"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="40dp"
android:paddingTop="8dp"
android:text="\nButton"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
`
then AndroidButtonWithIconAndText.java class
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class AndroidButtonWithIconAndText extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.android_button_with_icon_text);
}
}
and finally it may look like this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#000" />
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#fff" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#fff" />
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#000" />
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#fff" />
</LinearLayout>

Related

How to open new activity on click text view?

I am very new in application development. I am trying to open new activity when I click on TextView in Android Studio. But i am getting error from android studio which is below
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null
object reference at
com.example.etechnomateapp.MainActivity.onCreate(MainActivity.java:41)
The LAYOUT page
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/screenbg"
tools:context=".loginActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10sp"
android:orientation="vertical">
<ImageView
android:id="#+id/img"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20sp"
android:src="#drawable/shop_logo"
tools:ignore="ContentDescription"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40sp"
android:layout_marginBottom="40sp"
android:gravity="center_horizontal"
android:text="#string/user_login"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="#color/colorDarkRed"
android:textSize="22sp"
/>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/userEmailWreapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorRed"
android:layout_marginTop="5dp"
>
<EditText
android:id="#+id/emailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:layout_marginTop="5dp"
android:inputType="textEmailAddress"
android:hint="#string/enter_email"
android:textColor="#color/colorRed"
android:textSize="18sp"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/userPasswordWreapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorRed"
android:layout_marginTop="5dp"
>
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:layout_marginTop="5dp"
android:hint="#string/enter_password"
android:textColor="#color/colorRed"
android:inputType="textPassword"
android:textSize="18sp"/>
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="#+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp"
android:text="#string/user_login"
android:background="#drawable/btnloginbg"
android:textColor="#color/colorWhite"
android:textSize="20sp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:layout_marginBottom="10sp"
android:gravity="center_horizontal"
android:text="Or"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="#color/colorDarkRed"
android:textSize="22sp"
/>
<TextView
android:id="#+id/forgetPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/forgetPass"
android:textAlignment="center"
android:textAllCaps="false"
android:layout_marginLeft="45dp"
android:textColor="#color/colorDarkRed"
android:textSize="16sp"
/>
<TextView
android:id="#+id/UserSignup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/signup"
android:textAlignment="center"
android:textAllCaps="false"
android:layout_marginLeft="250dp"
android:layout_marginTop="-20dp"
android:textColor="#color/colorDarkRed"
android:textSize="16sp"
/>
</LinearLayout>
</RelativeLayout>
The Main Acxtivity Page
package com.example.etechnomateapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btnUsersignup, btnUserlogin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnUserlogin=findViewById(R.id.btnUserLogin);
btnUsersignup=findViewById(R.id.btnSignup);
final TextView register = (TextView) findViewById(R.id.UserSignup);
btnUsersignup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent= new Intent(MainActivity.this, RegistrationActivity.class);
startActivity(intent);
}
});
btnUserlogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent= new Intent(MainActivity.this, loginActivity.class);
startActivity(intent);
}
});
register.setOnClickListener(new View.OnClickListener() { /// errors occuring here
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, RegistrationActivity.class);
startActivity(myIntent);
}
});
}
}
This is a result of activity_main.xml not containing Textview by id UserSignup.
It seems like you have posted some other xml. Because there is no "btnUserLogin" in xml you have written in java code.

android.view.InflateException: Binary XML file line #0: Error inflating class <unknown>

Here is my fooddetail activity codes
public class FoodDetail extends AppCompatActivity {
TextView food_name,food_price,food_description;
ImageView food_image;
CollapsingToolbarLayout collapsingToolbarLayout;
FloatingActionButton btnCart;
ElegantNumberButton numberButton;
String foodId="";
FirebaseDatabase database;
DatabaseReference food;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_detail);
database = FirebaseDatabase.getInstance();
food = database.getReference("Food");
numberButton = (ElegantNumberButton)findViewById(R.id.number_button);
btnCart = (FloatingActionButton)findViewById(R.id.btnCart);
food_name = (TextView)findViewById(R.id.food_name);
food_description = (TextView)findViewById(R.id.food_description);
food_price = (TextView)findViewById(R.id.food_price);
food_image = (ImageView) findViewById(R.id.img_food);
collapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapsing);
collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppBar);
collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppBar);
if (getIntent() != null)
foodId = getIntent().getStringExtra("FoodId");
if(!foodId.isEmpty())
{
getDetailFood(foodId);
}
}
private void getDetailFood(String foodId)
{
food.child(foodId).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Food food = dataSnapshot.getValue(Food.class);
Picasso.with(getBaseContext()).load(food.getImage()).into(food_image);
collapsingToolbarLayout.setTitle(food.getName());
food_price.setText(food.getPrice());
food_name.setText(food.getName());
food_description.setText(food.getDescription());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
and here is my xml
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.order.m.jersonsordering.FoodDetail">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Base.ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true"
android:id="#+id/app_bar_layout">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/collapsing"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="#0e0d0e">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#null"
app:layout_collapseMode="parallax"
android:id="#+id/img_food"
android:scaleType="centerCrop" />
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="parallax"
android:id="#+id/toolbar"
app:title="Food Name">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnCart"
android:src="#drawable/ic_shopping_cart_black_24dp"
android:backgroundTint="#android:color/white"
android:elevation="6dp"
app:pressedTranslationZ="12dp"
app:layout_anchor="#id/app_bar_layout"
app:layout_anchorGravity="bottom|right|end"
app:useCompatPadding="true"/>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/nestedScrollView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/food_name"
android:layout_marginTop="8dp"
android:padding="12dp"
android:textColor="#000000"
android:text="Food Name"
android:textSize="21sp"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/layout_price"
android:orientation="horizontal">
<TextView
android:layout_width="1dp"
android:layout_weight="9"
android:layout_height="wrap_content"
android:id="#+id/food_price"
android:layout_marginTop="8dp"
android:padding="12dp"
android:textColor="#000000"
android:text="1,000PHP"
android:textStyle="bold"
android:textSize="15sp"/>
</LinearLayout>
<com.cepheuen.elegantnumberbutton.view.ElegantNumberButton
android:layout_width="100dp"
android:layout_height="30dp"
android:id="#+id/number_button"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginBottom="18dp"
app:textSize="8sp"
app:backGroundColor="#color/colorAccent"
app:textColor="#000000"
app:initialNumber="1"
app:finalNumber="20">
</com.cepheuen.elegantnumberbutton.view.ElegantNumberButton>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/food_description"
android:layout_marginTop="12dp"
android:lineSpacingMultiplier="1.5"
android:padding="12dp"
android:text="Description"
android:textColor="#000000"
android:textSize="14sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
I dont know what is causing the error and i cant resolve it I tried to rebuild and clean the project
I also tried searching for similar error but none is showing
the app keeps on closing and then thats the error(attached photo)
pls help me thank you :)
First make sure that you have included design library implementation 'com.android.support:design:26.+' in your application level build.gradle.
Then replace:
<android.support.design.widget.FloatingActionButton
...
...
android:backgroundTint
/>
with
<android.support.design.widget.FloatingActionButton
...
...
app:backgroundTint
/>

How to set color in odd and even position in recyclerview?

Position break in recyclerview
(https://drive.google.com/file/d/1EZADHjrpn8ZwryvQ6qPf1xU1hcG0-Mgf/view?usp=drivesdk)
You can get this by following manner.
Add this code to your onBindViewHolder() method of adapter. Like this:
#Override
public void onBindViewHolder(ViewHolder holder, int position)
{
if(position % 2 == 0)
{
//holder.rootView.setBackgroundColor(Color.BLACK);
holder.rootView.setBackgroundResource(R.color.black);
}
else
{
//holder.rootView.setBackgroundColor(Color.WHITE);
holder.rootView.setBackgroundResource(R.color.white);
}
}
After that surround your layout xml file that contains card design for example like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rootView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="72dp"
android:background="#color/my_white"
android:orientation="horizontal">
<ImageView
android:id="#+id/folder_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:src="#drawable/folder_icon" />
<TextView
android:id="#+id/folder_name"
android:layout_width="190dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:text="The Mills"
android:textColor="#color/my_blue"
android:textSize="16sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/folder_content_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:layout_marginLeft="12dp"
android:src="#drawable/folder_content_icon" />
<TextView
android:id="#+id/content_number"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:gravity="left"
android:text="3"
android:textColor="#color/my_blue"
android:textSize="16sp"
android:textStyle="bold" />
After that create variable in your View Holder class of adapter like this:
public static class ViewHolder extends RecyclerView.ViewHolder
{
LinearLayout rootView;//newly added field
public ViewHolder(View view)
{
super(view);
rootView=(LinearLayout)view.findViewById(R.id.rootView);
}
}
It will work for you to get the required results.

how to retrieve string value from dynamic edit text..?

In my layout I added two edit text by click on the add button and its added(works) and when I click on delete button it also delete the dynamic edit text.My question is how to get string value from it suppose I dynamically add two views and also delete when not needed.
here is my code..
public class PartDetails extends AppCompatActivity {
// Parent view for all rows and the add button.
private LinearLayout mContainerView;
// The "Add new" imageButton
private ImageButton mAddImageButton;
Button submit_part_details;
private View mExclusiveEmptyView;
EditText prequired,pnumber;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_part_details);
mContainerView = (LinearLayout) findViewById(R.id.parentView);
submit_part_details = (Button) findViewById(R.id.submit_part_details);
mAddImageButton = (ImageButton) findViewById(R.id.add_et_parts);
prequired= (EditText) findViewById(R.id.et_Prequired);
pnumber= (EditText) findViewById(R.id.et_Pnumber);
mAddImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
inflateEditRow();
}
});
submit_part_details.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String parts=prequired.getText().toString();
String pnum =pnumber.getText().toString();
}
});
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// TODO: Handle screen rotation:
// encapsulate information in a parcelable object, and save it
// into the state bundle.
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// TODO: Handle screen rotation:
// restore the saved items and inflate each one with inflateEditRow;
}
// Helper for inflating a row
private void inflateEditRow() {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row_add_parts, null);
final EditText parts_required = (EditText) addView
.findViewById(R.id.et_parts);
final EditText parts_number = (EditText) addView
.findViewById(R.id.et_Pnumber);
final ImageButton deleteButton = (ImageButton) addView
.findViewById(R.id.delete_et_parts);
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((LinearLayout) addView.getParent()).removeView(addView);
}
});
mContainerView.addView(addView);
}
}
this is parent XML layout
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#android:color/black"
android:layout_height="match_parent"
android:focusableInTouchMode="true">
<LinearLayout
android:id="#+id/parentView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:layout_below="#+id/tv"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/relative_layout1"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/parts_required"
android:layout_width="110dp"
android:layout_height="40dp"
android:text="Parts Required"
android:textColor="#fff"
android:textSize="15sp"
android:textStyle="bold"
android:background="#drawable/shape"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/parts_required"
android:layout_marginLeft="20dp"
android:orientation="horizontal">
<EditText
android:id="#+id/et_Prequired"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.8"
android:hint="text"
android:singleLine="true"
android:inputType="text"
android:textSize="20sp"
android:paddingLeft="20dp"
android:imeOptions="actionNext"
android:background="#drawable/shape_edit_text"
/>
<ImageButton
android:id="#+id/add_et_parts"
android:layout_width="0dp"
android:layout_height="40dp"
android:background="#null"
android:onClick="onAddNewClicked"
android:src="#android:drawable/ic_input_add"
android:layout_weight="0.2"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relative_layout2"
android:layout_marginTop="5dp"
android:layout_below="#+id/relative_layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/parts_number"
android:layout_width="110dp"
android:layout_height="40dp"
android:text="Parts Number"
android:textColor="#fff"
android:textSize="15sp"
android:textStyle="bold"
android:background="#drawable/shape"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/parts_number"
android:layout_marginLeft="20dp"
android:orientation="horizontal">
<EditText
android:id="#+id/et_Pnumber"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:hint="text"
android:singleLine="true"
android:inputType="text"
android:textSize="20sp"
android:paddingLeft="20dp"
android:imeOptions="actionNext"
android:background="#drawable/shape_edit_text"
/>
<!-- <ImageButton
android:id="#+id/add_et_parts_number"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="#null"
android:src="#android:drawable/ic_input_add"
android:layout_weight="0.2"/>-->
</LinearLayout>
</RelativeLayout>
<Button
android:layout_marginTop="50dp"
android:id="#+id/submit_part_details"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="#+id/snap"
android:textAllCaps="false"
android:textSize="20sp"
android:textColor="#fff"
android:background="#1c4648"
android:text="Submit All Details" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
this is Add Button XML layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#android:color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/relative_layout1"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/parts_required"
android:layout_width="110dp"
android:layout_height="40dp"
android:text="Parts Required"
android:textColor="#fff"
android:textSize="15sp"
android:textStyle="bold"
android:background="#drawable/shape"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/parts_required"
android:layout_marginLeft="20dp"
android:orientation="horizontal">
<EditText
android:id="#+id/et_parts"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.8"
android:singleLine="true"
android:inputType="text"
android:textSize="20sp"
android:hint="text"
android:paddingLeft="20dp"
android:imeOptions="actionNext"
android:background="#drawable/shape_edit_text"
/>
<ImageButton
android:id="#+id/delete_et_parts"
android:layout_width="0dp"
android:layout_height="40dp"
android:background="#null"
android:src="#android:drawable/ic_delete"
android:layout_weight="0.2"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relative_layout2"
android:layout_marginTop="5dp"
android:layout_below="#+id/relative_layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/parts_number"
android:layout_width="110dp"
android:layout_height="40dp"
android:text="Parts Number"
android:textColor="#fff"
android:textSize="15sp"
android:textStyle="bold"
android:background="#drawable/shape"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/parts_number"
android:layout_marginLeft="20dp"
android:orientation="horizontal">
<EditText
android:id="#+id/et_Pnumber"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:hint="text"
android:singleLine="true"
android:inputType="text"
android:textSize="20sp"
android:paddingLeft="20dp"
android:imeOptions="actionNext"
android:background="#drawable/shape_edit_text"
/>
<!-- <ImageButton
android:id="#+id/add_et_parts_number"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="#null"
android:src="#android:drawable/ic_input_add"
android:layout_weight="0.2"/>-->
</LinearLayout>
</RelativeLayout>
</LinearLayout>
ArrayList<EditText> edtParts_required=new ArrayList<EditText>();
ArrayList<EditText> edtParts_number=new ArrayList<EditText>();
// Helper for inflating a row
private void inflateEditRow() {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row_add_parts, null);
final EditText parts_required = (EditText) addView
.findViewById(R.id.et_parts);
final EditText parts_number = (EditText) addView
.findViewById(R.id.et_Pnumber);
//Add EditText object to Collection
edtParts_required.add(parts_required);
edtParts_number.add(parts_required);
final ImageButton deleteButton = (ImageButton) addView
.findViewById(R.id.delete_et_parts);
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((LinearLayout) addView.getParent()).removeView(addView);
}
});
mContainerView.addView(addView);
}
When you delete the inflated row remove the element from collection too
Loop through your Collection and get the text from dynamic EditText
for(int i=0;i<edtParts_required.size();i++){
String enterdText=edtParts_required.get(i).getText().toString();
}
you can do it in many ways . here are two of them:
make an ArrayList and whenever you add new edit text add it to this array too and when you delete remove it from here too. when you want to get their text put it in a for loop
BETTER WAY : you have a linear layout. get its child count and put it in for loop. something like this :
for (int i=0;i<mContainerView.getChildCount();i++){
View mView=mContainerView.getChildAt(i);
EditText myEditText=(EditText) mView.findViewById(R.id.et_parts);
String txt=myEditText.getText().toString();
EditText myEditText2=(EditText) mView.findViewById(R.id.et_Pnumber);
String txt2=myEditText2.getText().toString();
}

how to connect two layouts in eclips

I'm beginner in Java Android developing. i have two XML file in res/layout/. named activity_fast_tip.xml and activity_setting.xml i can't connect these two component together. in activity_setting.xml
#Override
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
setContentView(R.layout.activity_fast_tip);
tipPctTextView=(TextView)this.findViewById(R.id.tipPctTextView);
tipAmountTextView=(TextView)this.findViewById(R.id.tipAmtTextView);
totalAmountTextView=(TextView)this.findViewById(R.id.totalAmtTextView);
calcTipAmountButton=(Button)this.findViewById(R.id.calcTipButton);
billAmountTextView=(EditText)this.findViewById(R.id.billAmtEditText);
calcTipAmountButton.setOnClickListener(new onClickListener()
{
#Override
public void onClick(View v) {
calculateTip();
}
});
}
and in activity_fast_tip
<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: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=".FastTipActivity" >
<EditText
android:id="#+id/billAmtEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:ems="10"
android:gravity="right|center_vertical"
android:hint="#string/billAmount"
android:inputType="number|numberSigned|numberDecimal" >
<requestFocus />
</EditText>
<Button
android:id="#+id/calcTipButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/billAmtEditText"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="#string/calculateTip" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/billAmtEditText"
android:layout_below="#id/calcTipButton"
android:layout_marginTop="18dp"
android:text="#string/tipPercentage"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/TextView01"
android:layout_below="#+id/TextView01"
android:layout_marginLeft="0dp"
android:layout_marginTop="18dp"
android:text="#string/tipAmount"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="18dp"
android:text="#string/totalAmount"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FF0000" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/billAmtEditText"
android:layout_marginBottom="35dp"
android:text="#string/button" />
use layout inflater
http://developer.android.com/reference/android/view/LayoutInflater.html
or Include
<include
android:layout_width="fill_parent"
android:layout_height="fill_parent"
layout="#layout/activity_setting"
/>
u can use include tag a layout like this into another layout
titlebar.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width=”match_parent”
android:layout_height="wrap_content"
android:background="#color/titlebar_bg">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/gafricalogo" />
</FrameLayout>
some other layout where we include that titlebar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background="#color/app_bg"
android:gravity="center_horizontal">
<include layout="#layout/titlebar"/>
<TextView android:layout_width=”match_parent”
android:layout_height="wrap_content"
android:text="#string/hello"
android:padding="10dp" />
...
</LinearLayout>
http://developer.android.com/training/improving-layouts/reusing-layouts.html
2. u can use LayoutInflater
code snippet
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View someView = inflater.inflate(R.layout.ur_layout,null); //2nd parameter is for viewgroup
which_layout_u_want_to_add.addView(someView);
http://developer.android.com/reference/android/view/LayoutInflater.html

Resources