xml layout won't display on emulator - android-studio

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

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.

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