I am new to android and I am trying to learn by making a simple chat app in android studio.
I started by creating a navigation drawer. Now I want to add tabs to my app like this - chats / contact / calls below navigation drawer, but I could not find a simple step by step tutorial for doing this.
Before I started this chat app I worked on a simpler app and added some tabs to that old app like this:
public class MainActivity extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources resources = getResources();
TabHost tabHost = getTabHost();
// First tab
Intent intentTabOne = new Intent().setClass(this, TabOneActivity.class);
TabSpec tabSpecTabOne = tabHost
.newTabSpec("Tab One")
.setIndicator("", resources.getDrawable(R.drawable.icon_one_config))
.setContent(intentTabOne);
// Second tab
Intent intentTabTwo = new Intent().setClass(this, TabTwoActivity.class);
TabSpec tabSpecSecondTab = tabHost
.newTabSpec("Tab Two")
.setIndicator("", resources.getDrawable(R.drawable.icon_two_config))
.setContent(intentTabTwo);
// Third tab
Intent intentTabThree = new Intent().setClass(this, TabThree.class);
TabSpec tabSpecSent = tabHost
.newTabSpec("Tab Three")
.setIndicator("", resources.getDrawable(R.drawable.icon_three_invitations_config))
.setContent(intentTabThree);
// add all tabs
tabHost.addTab(tabSpecTabOne);
tabHost.addTab(tabSpecTabTwo);
tabHost.addTab(tabSpecTabThree);
//set Windows tab as default
tabHost.setCurrentTab(0);
}
But now my main activity looks like this:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed()...
Also, since TabActivity is now deprecated and I think I should use fragment, I got a little bit lost. I would really appreciate if someone could point me in the right direction. Thanks in advance.
I'd suggest to use a TabLayout that you can put below your Toolbar/Actionbar, so you can use it in parallel with the navigation drawer.
This answer shows how setup the Tabbar with a ViewPager. In short: You need to include the Design Support Library to use the TabLayout. In your XML layout, put the TabLayout and the ViewPager somewhere below the Toolbar, such that it fits your intentions. Then you can setup the different tabs with their own Fragments in your Activity.
Related
I have a custom list item XML with 2 Buttons and I want to get when one is clicked (and a way of knowing which one). My ListView is made with data from a database. This is what I use for populating the list:
private DBManager manager;
private Cursor cursor;
private ListView lista;
SimpleCursorAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.m_home_layout);
manager = new DBManager(this);
lista = (ListView) findViewById(R.id.listview);
String[] from = new String[]{manager.CN_NAME, manager.CN_DESCRIPTION, manager.CN_TIME, manager.CN_DAY};
int[] to = new int[] {R.id.list_title, R.id.list_description, R.id.list_time, R.id.list_day};
cursor = manager.cargarCursorData("data");
adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, from, to,0);
lista.setAdapter(adapter);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
I've seen people saying that you have to use bindView but I don't have anything like that and I don't know what it is. Sorry for my ignorance.
You can go through this tutorials for Creating Custom Adapter, in which you can get the which Button is clicked:
http://www.survivingwithandroid.com/2012/10/android-listview-custom-adapter-and.html
http://www.journaldev.com/10416/android-listview-with-custom-adapter-example-tutorial
I am trying to work with IBM Watson's Android speech to text library. I noticed there was a similar question here but no answer was given. I have everything integrated but when trying to start the recognize service, it gets this error:
java.lang.UnsatisfiedLinkError: Native library (com/sun/jna/android-aarch64/libjnidispatch.so) not found in resource path (.)
at com.sun.jna.Native.loadNativeDispatchLibraryFromClasspath(Native.java:786)
at com.sun.jna.Native.loadNativeDispatchLibrary(Native.java:746)
at com.sun.jna.Native.<clinit>(Native.java:135)
at com.sun.jna.NativeLibrary.<clinit>(NativeLibrary.java:82)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:337)
at com.ibm.watson.developer_cloud.android.speech_to_text.v1.opus.JNAOpus.<clinit>(JNAOpus.java:42)
at com.ibm.watson.developer_cloud.android.speech_to_text.v1.audio.OggOpusEnc.initEncoderWithUploader(OggOpusEnc.java:53)
at com.ibm.watson.developer_cloud.android.speech_to_text.v1.audio.WebSocketUploader.initStreamAudioToServer(WebSocketUploader.java:113)
at com.ibm.watson.developer_cloud.android.speech_to_text.v1.audio.WebSocketUploader.access$000(WebSocketUploader.java:46)
at com.ibm.watson.developer_cloud.android.speech_to_text.v1.audio.WebSocketUploader$2.run(WebSocketUploader.java:175)
07-23 12:51:36.854 27413-27757/com.intellidev.mobitranscribe E/AudioRecord: AudioFlinger could not create record track, status: -1
07-23 12:51:36.874 27413-27757/com.intellidev.mobitranscribe E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1.
07-23 12:51:36.874 27413-27757/com.intellidev.mobitranscribe E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
07-23 12:51:36.924 27413-27757/com.intellidev.mobitranscribe E/AudioCaptureThread: Error reading voice audio
java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
at android.media.AudioRecord.startRecording(AudioRecord.java:943)
at com.ibm.watson.developer_cloud.android.speech_to_text.v1.audio.AudioCaptureThread.run(AudioCaptureThread.java:62)
My code is here:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
txtSpeechInput = (TextView) findViewById(R.id.textINput);
btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//promptSpeechInput();
//set();
SpeechToText.sharedInstance().recognize();
}
});
btnEnd = (ImageButton) findViewById(R.id.btnEnd);
btnEnd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SpeechToText.sharedInstance().stopRecording();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
// Configuration
SpeechConfiguration sConfig = new SpeechConfiguration(SpeechConfiguration.AUDIO_FORMAT_OGGOPUS);
// STT
SpeechToText.sharedInstance().initWithContext(this.getHost("https://gateway.watsonplatform.net/conversation/api"), this.getApplicationContext(), sConfig);
SpeechToText.sharedInstance().setCredentials("PERSONAL","INFO");
SpeechToText.sharedInstance().setDelegate(this);
I needed to use my code as below, which is gotten from the nuance sample
Session session = Session.Factory.session(this, com.intellidev.mobitranscribe.Configuration.SERVER_URI,
com.intellidev.mobitranscribe.Configuration.APP_KEY);
Transaction.Options options = new Transaction.Options();
options.setRecognitionType(RecognitionType.DICTATION);
options.setDetection(DetectionType.Long);
options.setLanguage(new Language("ENG-USA"));
//Start listening
recoTransaction = speechSession.recognize(options, recoListener);
So I'm kind of on the new side with Android Studio and Android programming as a whole. I'm currently working on an app that (where one of the features) will pull a user's profile picture from Facebook for example, and change the ImageView in the Navigation Drawer accordingly (for example on the Gmail app where it shows your G+ profile picture). The Facebook bit can be left for another day, however I've been stuck with a certain issue for the past 2 and a half hours, and might need some assistance. My onCreate method looks like the following:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View headerView = navigationView.inflateHeaderView(R.layout.nav_header_main);
imgProfilePic = (ImageView) headerView.findViewById(R.id.imgProfilePic);
imgProfilePic.setImageResource(R.drawable.troll_face);
}
A lot of my onCreate method is still pretty "vanilla", the bit where I think the issue lies is before NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);. My main issue I'm experiencing here is that 2 headers now show up below each other in my Navigation Drawer, the regular one before the .setImageResource() was done, and the one (where the resource was changed) after it.
I have done a lot of Googling with regards to this issue, and some people mentioned it was due to a bug in Android Studio.
Any assistance with my problem would be greatly appreciated.
In my application I show picture in this way:
if (mNavigationView.getHeaderCount() > 0) {
ImageView mBackground = (ImageView) mNavigationView.getHeaderView(0).findViewById(R.id.user_background);
CircleImageView mUserIcon = (CircleImageView) mNavigationView.getHeaderView(0).findViewById(R.id.user_icon);
Glide.with(this).load(R.drawable.background).centerCrop().into(mBackground);
Glide.with(this).load(R.drawable.me).centerCrop().into(mUserIcon);
}
The important thing that I'd advise for you is using Glide even for settings the picture from resources. This library compresses the picture size and thus makes your app work much faster.
About the question - you have to look for the first and only header. That's the trick in this issue :)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//identify object
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
**title = (TextView)findViewById(R.id.theTitle);
Intent intent = getIntent();
String Title = intent.getExtras().getString("name");
title.setText(Title);**
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
Intent intent = new Intent(this, addCounter.class);
startActivity(intent);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
When nav_gallery = true, it brings me to activity2 which has the following code:
public void addOk(){
String sendName = name.getText().toString();
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("name", sendName);
startActivity(intent);
}
I want to send the information from activity2 back to the main activity. Is my getIntent() in the wrong place?
Your getIntent() method is inside the onCreate method.
But onCreate is called only once, at creation. And if the MainActivity is still in the stack, there is no reason why onCreate() would be called again.
You should try to insert your code inside onResume() or onStart().
I'm fairly new to android programming and developed an app that has a Navigation Drawer implemented. Per Google guidelines, i'd like the NavDraw to start open but am unable to do this.
This is my onCreate (I guess this is where I should implement this feature, right?)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, menuEntries);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView navList = (ListView) findViewById(R.id.drawer);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
drawerToggle = new ActionBarDrawerToggle(
this,
drawer,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
)
(...)
Use drawer.openDrawer(Gravity.LEFT);
You can use
drawer.openDrawer(Gravity.LEFT);
or
drawer.openDrawer(Gravity.RIGHT`);