IllegalArgumentException: ID does not reference a View inside this Activity - android-studio

I have been trying to make a home page for my app which contains some modern dashboard along with navigation drawer.
Error found in my code:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.thinkerlab, PID: 13956
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.thinkerlab/com.example.thinkerlab.HomePage}: java.lang.IllegalArgumentException: ID does not reference a View inside this Activity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2517)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2577)
at android.app.ActivityThread.access$1000(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1414)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5619)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:853)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:737)
Caused by: java.lang.IllegalArgumentException: ID does not reference a View inside this Activity
at androidx.core.app.ActivityCompat.requireViewById(ActivityCompat.java:368)
at androidx.navigation.Navigation.findNavController(Navigation.java:58)
at com.example.thinkerlab.HomePage.onCreate(HomePage.java:70)
at android.app.Activity.performCreate(Activity.java:6351)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1114)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2470)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2577) 
at android.app.ActivityThread.access$1000(ActivityThread.java:166) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1414) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5619) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:853) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:737) 
AndroidManifest.xml file of my project:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Page1"></activity>
<activity android:name=".Startpage" />
<activity
android:name=".HomePage"
android:label="#string/title_activity_home_page"
android:theme="#style/AppTheme.NoActionBar" />
<activity android:name=".ResetPassword" />
<activity android:name=".RegistrationActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="#array/preloaded_fonts" />
</application>
HomePage.java file of my project:
package com.example.thinkerlab;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.Menu;
import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.core.view.GravityCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
public class HomePage extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawerLayout;
NavigationView navigationView;
Toolbar toolbar;
ActionBar actionBar;
Activity activity;
private AppBarConfiguration mAppBarConfiguration;
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
toolbar = findViewById(R.id.toolbar2);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
Menu menu=navigationView.getMenu();
menu.findItem(R.id.nav_Log_out).setVisible(true);
menu.findItem(R.id.nav_Profile).setVisible(true);
navigationView.bringToFront();
ActionBarDrawerToggle toggle= new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
navigationView.setCheckedItem(R.id.nav_home);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawerLayout)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
public void onBackPressed()
{
if(drawerLayout.isDrawerOpen(GravityCompat.START))
{
drawerLayout.closeDrawer(GravityCompat.START);
}
else
{
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home_page, menu);
return true;
}
#Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId())
{
case R.id.nav_home:
break;
case R.id.nav_Science:
startActivity(new Intent(HomePage.this,Page1.class));
break;
case R.id.nav_Contact_Us:
Toast.makeText(this,"Share",Toast.LENGTH_SHORT).show();
break;
case R.id.nav_Log_out:
startActivity(new Intent(HomePage.this,MainActivity.class));
break;
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
}
styles.xml file of my project:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
My activity_home_page xml file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/header"
app:menu="#menu/main_menu" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/iv1"
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView2"
app:layout_constraintTop_toBottomOf="#+id/toolbar2"
app:srcCompat="#drawable/p" />
<TextView
android:id="#+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:fontFamily="sans-serif-condensed-medium"
android:text="Let's Think and Build"
android:textSize="18sp"
app:layout_constraintEnd_toStartOf="#+id/iv1"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:fontFamily="serif"
android:text="Thinker Lab"
android:textSize="36sp"
app:layout_constraintEnd_toStartOf="#+id/iv1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar2" />
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#04FFFFFF"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
tools:layout_editor_absoluteX="1dp"
tools:ignore="MissingConstraints" />
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="0dp"
android:layout_height="175dp"
android:orientation="horizontal"
android:padding="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/iv1">
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/iv2"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
app:srcCompat="#drawable/science" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/iv2"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:fontFamily="#font/aclonica"
android:text="Science"
android:textSize="18sp" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/iv3"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
app:srcCompat="#drawable/maths" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/iv3"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:fontFamily="#font/aclonica"
android:text="Mathematics"
android:textSize="18sp" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="175dp"
android:orientation="horizontal"
android:padding="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout2">
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/iv4"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
app:srcCompat="#drawable/engg" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/iv4"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:fontFamily="#font/aclonica"
android:text="Engineering"
android:textSize="18sp" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/iv5"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
app:srcCompat="#drawable/tech" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/iv5"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:fontFamily="#font/aclonica"
android:text="Technology"
android:textSize="18sp" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.drawerlayout.widget.DrawerLayout>
Please Help me find a solution to this. I am stuck in this for over a week now.

I also encountered this problem when I used multi-module development. After investigation, I found that it was because I created an XML with the same name in another module. There is no in that XML, so change the name to solve it.

you are using this nav controller
mNavController = findNavController(R.id.nav_host_fragment_activity_main)
you need to use this supportFragmentManager nav controller
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment_activity_main)
as NavHostFragment
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mNavController = navHostFragment.navController
xml code looks like this
<androidx.fragment.app.FragmentContainerView
android:id="#+id/nav_host_fragment_activity_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/mobile_navigation" />

It's all about file naming. Recall what files have you added/replaced/renamed just recently and roll it back.

Related

error: activity not declared in AndroidManifest.xml in Android Studio

My AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="auto">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-feature android:name="com.softbank.hardware.pepper" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.TammyApp">
<activity android:name=".ui.categories.CategoriesActivity">
</activity>
# <activity android:name=".activities.Muse.muse_pick_duration" />
<activity android:name=".activities.Welcome">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:enabled="true" android:name=".activities.SpeechService" />
</application>
</manifest>
/////////////////////////////////
muse_pick_duration.kt:
package com.tammy.tammyapp.activities.Muse
import com.tammy.tammyapp.R
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.tammy.tammyapp.activities.TutorialActivity
abstract class muse_pick_duration : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
setContentView(R.layout.muse_pick_duration)
super.onCreate(savedInstanceState)
}
}
/////////////////////////////////////////////////////////
museHome.kt:
package com.tammy.tammyapp.activities.Muse
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.tammy.tammyapp.R
import com.tammy.tammyapp.activities.TutorialActivity
abstract class MipniHome : AppCompatActivity(){
//abstract val muse_pick_duration: Unit
abstract val muse_pick_duration: Unit
abstract val savedInstanceState: Bundle?
fun onCreate() {
setContentView(R.layout.muse_home)
super.onCreate(savedInstanceState)
val button3 = findViewById<Button>(R.id.button3)
button3.setOnClickListener {
val Intent = Intent(this, muse_pick_duration::class.java)
startActivity(Intent)
}
}
}
///////////////////////////////////////////////////////
muse_home.xml:
<?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">
tools:context=".museHome">
// main menu button
<Button
android:id="#+id/button"
android:layout_width="100dp"
android:layout_height="60dp"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="121dp"
android:layout_marginBottom="259dp"
android:backgroundTint="#3F51B5"
android:text="Main Menu"
android:textSize="16sp"
app:cornerRadius="10dp"
app:layout_constraintBottom_toTopOf="#+id/button2"
app:layout_constraintEnd_toStartOf="#+id/textView4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
// back button
<Button
android:id="#+id/button2"
android:layout_width="100dp"
android:layout_height="60dp"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"
android:backgroundTint="#3F51B5"
android:text="Back"
android:textSize="16sp"
app:cornerRadius="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
// next button
<Button
android:id="#+id/button3"
android:layout_width="100dp"
android:layout_height="60dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:backgroundTint="#3F51B5"
android:text="Next"
android:textSize="16sp"
app:cornerRadius="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/textView3"
app:layout_constraintTop_toBottomOf="#+id/imageView"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="#+id/textView2"
android:layout_width="216dp"
android:layout_height="56dp"
android:layout_marginStart="12dp"
android:layout_marginBottom="16dp"
android:text="Press "BACK" to return to the previous screen"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/button2" />
<TextView
android:id="#+id/textView3"
android:layout_width="218dp"
android:layout_height="47dp"
android:layout_marginEnd="124dp"
android:layout_marginBottom="24dp"
android:text="Press "NEXT" when you are ready to continue"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="#+id/textView4"
android:layout_width="508dp"
android:layout_height="63dp"
android:layout_marginTop="80dp"
android:layout_marginEnd="237dp"
android:layout_marginBottom="266dp"
android:text="Welcome to MIPNI,"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="50sp"
app:layout_constraintBottom_toTopOf="#+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/button"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/textView5"
android:layout_width="370dp"
android:layout_height="28dp"
android:text="your musical companion app!"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
<ImageView
android:id="#+id/imageView"
android:layout_width="502dp"
android:layout_height="256dp"
android:layout_marginBottom="23dp"
app:layout_constraintBottom_toTopOf="#+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView5"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="#drawable/music" />
</androidx.constraintlayout.widget.ConstraintLayout>
//////////////////////////////////////////////
I attached my code above that includes a button(button3) trying to open another activity(muse_pick_duration) and the AndroidManifest.xml.
The bolded activity is the one that says is not declared but I did declare it... IDK what is going on. Can someone please help?!
I tried so invalidating the cache and restaring. I also tried syncing project with gradle files but mone of that worked. I have two activities: muse_pick_duration and museHome and both are in kotlin...

Using Exoplayer to view video files but videos are not visible in custom_playback_control

Problem:
Video is not playing in the custom_playback_control.xml like image1
instead it is playing in a separate black screen like image2
Iam using exoplayer 2.16.1 as my dependency in build.gradle
I want to play the video with the playback_control.
This is my videoplayer.java
import androidx.appcompat.app.AppCompatActivity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.source.ConcatenatingMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import java.io.File;
import java.util.ArrayList;
public class videoplayer extends AppCompatActivity {
PlayerView playerView;
SimpleExoPlayer player;
int position;
String video_title;
ArrayList<MediaFiles> videosFiles = new ArrayList<>();
ConcatenatingMediaSource concatenatingMediaSource;
TextView title;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videoplayer);
playerView = findViewById(R.id.exoplayer_view);
getSupportActionBar().hide();
position = getIntent().getIntExtra("Position", 1);
video_title = getIntent().getStringExtra("Title");
videosFiles = getIntent().getExtras().getParcelableArrayList("videoArrayList");
title = findViewById(R.id.video_title);
title.setText(video_title);
playVideo();
}
private void playVideo() {
String path = videosFiles.get(position).getPath();
Uri uri = Uri.parse(path);
player = new SimpleExoPlayer.Builder(this).build();
DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "app_name"));
concatenatingMediaSource = new ConcatenatingMediaSource();
for (int i = 0; i < videosFiles.size(); i++) {
new File(String.valueOf(videosFiles.get(i)));
MediaSource mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(String.valueOf(uri)));
concatenatingMediaSource.addMediaSource(mediaSource);
}
playerView.setPlayer(player);
playerView.setKeepScreenOn(true);
player.prepare(concatenatingMediaSource);
player.seekTo(position, C.TIME_UNSET);
playError();
}
private void playError() {
player.addListener(new Player.EventListener() {
public void onPlayerError(ExoPlaybackException error) {
Toast.makeText(videoplayer.this, "Error in Playing Video", Toast.LENGTH_SHORT).show();
}
});
player.setPlayWhenReady(true);
}
}
This is my activity_videoplayer.xml
<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"
tools:context=".videoplayer">
<com.google.android.exoplayer2.ui.PlayerView
android:id="#+id/exoplayer_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
app:controller_layout_id="#layout/custom_playback_control"
app:hide_on_touch="true"
app:player_layout_id="#layout/exo_player_view"
app:resize_mode="fit"
app:show_timeout="40000"
app:shutter_background_color="#color/black"
app:surface_type="surface_view"
app:use_controller="true" />
</RelativeLayout>
This is my custom_playback_control.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:visibility="visible">
<ImageView
android:id="#+id/video_back"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="10dp"
android:src="#drawable/ic_back_button" />
<TextView
android:id="#+id/video_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="2"
android:text="File Name"
android:textColor="#color/white"
android:textSize="20sp" />
<ImageView
android:id="#+id/video_playlist"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:src="#drawable/ic_baseline_playlist_play_24" />
<ImageView
android:id="#+id/video_more"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:src="#drawable/ic_menu_vertical" />
</LinearLayout>
<RelativeLayout
android:id="#+id/progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/bottom_icons"
android:visibility="visible">
<TextView
android:id="#+id/exo_position"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="10dp"
android:text="00:00"
android:textColor="#color/white" />
<TextView
android:id="#+id/exo_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="10dp"
android:text="23:34"
android:textColor="#color/white" />
<com.google.android.exoplayer2.ui.DefaultTimeBar
android:id="#+id/exo_progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toStartOf="#id/exo_duration"
android:layout_toLeftOf="#id/exo_duration"
android:layout_toEndOf="#id/exo_position"
android:layout_toRightOf="#id/exo_position"
app:scrubber_drawable="#drawable/scrubber"
app:played_color="#color/purple_200"/>
</RelativeLayout>
<LinearLayout
android:id="#+id/bottom_icons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:weightSum="7">
<ImageView
android:id="#+id/unlock"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:src="#drawable/ic_lock_close" />
<ImageView
android:id="#+id/exo_prev"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:src="#drawable/ic_rewind_button" />
<ImageView
android:id="#+id/exo_rew"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:src="#drawable/ic_replay_10" />
<ImageView
android:id="#+id/exo_play"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_weight="1"
android:src="#drawable/ic_play_icon" />
<ImageView
android:id="#+id/exo_pause"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_weight="1"
android:src="#drawable/ic_pause_button" />
<ImageView
android:id="#+id/exo_ffwd"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:src="#drawable/ic_forward_10" />
<ImageView
android:id="#+id/exo_next"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:src="#drawable/ic_fast_forward_round" />
<ImageView
android:id="#+id/exo_more"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:src="#drawable/ic_menu_vertical" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>

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

Resources