Tuesday, 20 June 2017

Easiest way to use onClick method in Android. 

It can be done by using the XML. Just define a Button with 'id' and and 'onClick'.

Like: 
<Button 
android: height = "wrap_content"
android: height = "wrap_content"
android: text = "Click it"
android: id = "@id/click_button_1"
android: onClick = "Do_the_Click"
/>

So when you will define the onClick, there will be a suggestion on the left side of the statement. Click on that, it would suggest to make a method in java file with name "Do_the_Click", Click on it and it will create it.

And then you can write your code in that method. 

Sunday, 11 June 2017

How to import or use any defined thing in the MainActivity.java File?

The simple Answer is from Resources.
Now, What are resources and how to use Resources?

The Most simple and Standard way is to create a Variable and use that to get the other resources from other Files like Strings.xml.
_______________________________________________________________________________
Consider that had defined a String-Array in Strings.xml file and want to import that array into you ListView.

Step 1. Create a Listview in MainActivty.xml(Your main Layout file) and define ID.

Step 2. Define you string -array in Strings.xml file(It's located in Values directory) and give a name say "Languages".

Step 3. Go to MainActivity.java file and type following code

ListView list = (ListView) findViewById(R.id.list_items);

Resources resources = getResources();
String[] languages = resources.getStringArray(R.array.Languages);

ArrayAdapter<String> adapter =  new ArrayAdapter<String>(this, android.R.layout.Simple_list_1, languages); //I don't exactly know the ListView drop-down options
listview.setAdapter(adapter);

Step 4.  Run the Emulator.