How to open new activity on click text view? - android-studio

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.

Related

xml layout won't display on emulator

I've been working on an app, all the code seems to be fine, xml and java. When i run the app in an emulator, my splash screen is no longer running, when i click on a button, it takes me to the right page but does not show me the layout of the file.
When i edit the xml file layout, the changes are not being shown on the app when it runs in the emulator.
i'll post the code below:
This code is for the xml layout of the 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/bgapp"
tools:context=".complain">
<ImageView
android:layout_width="310dp"
android:layout_height="86dp"
android:layout_marginLeft="45dp"
android:src="#drawable/bfcfulllogotransparent" />
<ImageView
android:layout_width="79dp"
android:layout_height="76dp"
android:layout_marginLeft="160dp"
android:layout_marginTop="500dp"
android:src="#drawable/bfchandtransparent"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Making a Complaint"
android:textAllCaps="false"
android:layout_marginTop="75dp"
android:textAlignment="center"
android:textColor="#ffff"
android:textStyle="bold"
android:textSize="18dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_marginHorizontal="10dp"
android:textAlignment="center"
android:textColor="#ffff"
android:textAllCaps="false"
android:text="There may be times when you feel that you are not happy with the services you have been provided, it is your right to be able to make a complaint about these issues.
"
android:fontFamily="sans-serif"
android:textSize="16dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="190dp"
android:layout_marginHorizontal="10dp"
android:textAlignment="center"
android:textColor="#ffff"
android:textAllCaps="false"
android:text="There are a number of ways that you can make a complaint…"
android:fontFamily="sans-serif"
android:textSize="16dp"/>
<Button
android:id="#+id/butthow"
android:layout_width="273dp"
android:layout_height="66dp"
android:layout_marginLeft="55dp"
android:layout_marginTop="280dp"
android:background="#drawable/custom_button"
android:text="How to make a complaint"
android:textAllCaps="false"
android:textColor="#ffff"
android:textSize="18dp"
android:textStyle="bold"/>
<Button
android:id="#+id/buttwhat"
android:layout_width="273dp"
android:layout_height="66dp"
android:layout_marginLeft="55dp"
android:layout_marginTop="380dp"
android:background="#drawable/custom_button"
android:text="What happens to your complaint"
android:textAllCaps="false"
android:textColor="#ffff"
android:textSize="18dp"
android:textStyle="bold"/>
</RelativeLayout>
This code is for the xml for the splash screen:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bgapp"
tools:context=".splashscreen">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:text="Childrens Guide"
android:textColor="#ffff"
android:textSize="35dp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/textView4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView4" />
<TextView
android:id="#+id/textView4"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="214dp"
android:text="A guide for children living in foster care"
android:textAlignment="center"
android:textColor="#ffff"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/imageView5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="350dp"
android:layout_height="100dp"
android:layout_marginStart="15dp"
android:layout_marginTop="27dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="133dp"
android:src="#drawable/bfcfulllogotransparent"
app:layout_constraintBottom_toTopOf="#+id/imageView6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="101dp"
android:layout_height="0dp"
android:layout_marginBottom="41dp"
android:src="#drawable/bfchandtransparent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="1000dp"
android:layout_height="200dp"
android:layout_marginStart="18dp"
android:layout_marginEnd="18dp"
android:layout_marginBottom="173dp"
android:src="#drawable/coverkids"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView4" />
</androidx.constraintlayout.widget.ConstraintLayout>
this is the java for the splashscreen:
package com.example.bfcchildrensguidenew;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
public class splashscreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splashscreen);
Thread myThread = new Thread() {
#Override
public void run() {
try {
sleep(3000 );
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
myThread.start();
}
}
You can not call startActivity method outside of main thread(UI thread), it will crash.
User handler instead a Thread.
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
Intent intent = new Intent (getApplicationContext(),MainActivity.this);
startActivity(intent);
}
});

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
/>

Android Scrollview and layout problems

I have a question regarding android layouts. I cannot achieve what I am looking for and I need some direction.
I am looking for a layout that will be inside a Scroll View layout. its hard to explain but a picture is worth thousand words.enter image description here
I want this inside a scroll view, can any help please?
If you do not want to add images dynamically than this is the layout you want Try this.
activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="#+id/sv"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linMain"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<RelativeLayout
android:id="#+id/lin1"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_margin="10dp">
<ImageView
android:id="#+id/imgView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#android:color/holo_purple" />
<ImageView
android:id="#+id/imgView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#id/imgView1"
android:layout_marginTop="20dp"
android:background="#android:color/holo_red_light" />
<ImageView
android:id="#+id/imgView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#id/imgView2"
android:layout_marginTop="20dp"
android:background="#android:color/holo_green_light" />
<ImageView
android:id="#+id/imgView4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#id/imgView3"
android:layout_marginTop="20dp"
android:background="#android:color/holo_blue_light" />
</RelativeLayout>
<LinearLayout
android:id="#+id/lin2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/linImages"></WebView>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Here i am attaching screenshot of this layout UI.
Try this,I have made two layouts ..
1) activity_main that contains gridview and webview.
2)activity_gridview which contains imageview which will inflate in gridview.
I have added images dynamically to Gridview using BaseAdapter.
In MainActivity i have set that Adapter to GridView. Try this code once.
activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:fillViewport="false"
android:orientation="vertical">
<LinearLayout
android:id="#+id/linImages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid"
android:layout_width="100dp"
android:layout_height="match_parent"
android:numColumns="1"></GridView>
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/linImages"></WebView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
activity_gridview.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imgView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/star2" />
<ImageView
android:id="#+id/imgView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/star1" />
<ImageView
android:id="#+id/imgView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/star3" />
<ImageView
android:id="#+id/imgView4"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/star1" />
</LinearLayout>
CustomAdapter.java :
public class CustomAdapter extends BaseAdapter {
Context context;
int flags[];
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, int[] flags) {
this.context = applicationContext;
this.flags = flags;
inflter = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
return flags.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.activity_gridview, null);
ImageView imgView1 = (ImageView) view.findViewById(R.id.imgView1);
ImageView imgView2 = (ImageView) view.findViewById(R.id.imgView2);
ImageView imgView3 = (ImageView) view.findViewById(R.id.imgView3);
ImageView imgView4 = (ImageView) view.findViewById(R.id.imgView4);
imgView1.setImageResource(flags[0]);
imgView2.setImageResource(flags[1]);
imgView3.setImageResource(flags[2]);
imgView4.setImageResource(flags[3]);
return view;
}
}
MainActivity.java :
public class MainActivity extends AppCompatActivity {
int images[] = {R.drawable.star2, R.drawable.star1, R.drawable.star3, R.drawable.star1};
GridView grid;
CustomAdapter customAdapter;
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView= (WebView) findViewById(R.id.webView);
grid = (GridView) findViewById(R.id.grid);
customAdapter = new CustomAdapter(getApplicationContext(), images);
grid.setAdapter(customAdapter);
}
}

Android Grid Menu 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>

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();
}

Resources